This page is no longer maintained — Please continue to the home page at www.scala-lang.org

xml creation issue

3 replies
Xiaobo Yang
Joined: 2010-01-28,
User offline. Last seen 42 years 45 weeks ago.

Hi all,

I want to create an xml fragment like below. Services 1 and 2 are
retrieved from somewhere else.

I hope to define a method which accepts a parameter, the service name,
to create the xml fragment.

def addService(services: java.util.List, name: String): Elem = {
return <{name}>
// loop the list to add services

}

But the above code is not recognised by scala. How to fix it?

Many thanks,
Xiaobo

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: xml creation issue

i had the same problem. it seem you cannot create an XML literal where the outer scope element has a variable {} name. as a workaround you could use an attribute though:

def createXML( name: String ) =
...

other way is to modify the XML literal:

scala> def standardXML =
| 1
|
standardXML: scala.xml.Elem

scala> def namedXML( name: String ) = {
| val std = standardXML
| std.copy( label = name )
| }
namedXML: (name: String)scala.xml.Elem

scala> namedXML( "Hallo" )
res0: scala.xml.Elem =

1

ciao, -sciss-

Am 05.02.2010 um 13:13 schrieb Xiaobo Yang:

> Hi all,
>
> I want to create an xml fragment like below. Services 1 and 2 are
> retrieved from somewhere else.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> I hope to define a method which accepts a parameter, the service name,
> to create the xml fragment.
>
> def addService(services: java.util.List, name: String): Elem = {
> return <{name}>
> // loop the list to add services
>
> }
>
> But the above code is not recognised by scala. How to fix it?
>
> Many thanks,
> Xiaobo

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: xml creation issue
I hadn't thought to use Scala 2.8's copy method. It works as a single expression as well:   <xml/>.copy(label="blah")

On Fri, Feb 5, 2010 at 12:26 PM, Sciss <contact@sciss.de> wrote:
i had the same problem. it seem you cannot create an XML literal where the outer scope element has a variable {} name. as a workaround you could use an attribute though:

def createXML( name: String ) = <service name={name}>
       ...
</service>

other way is to modify the XML literal:

scala> def standardXML = <service>
    | <test>1</test>
    | </service>
standardXML: scala.xml.Elem

scala> def namedXML( name: String ) = {
    | val std = standardXML
    | std.copy( label = name )
    | }
namedXML: (name: String)scala.xml.Elem

scala> namedXML( "Hallo" )
res0: scala.xml.Elem =
<Hallo>
<test>1</test>
</Hallo>

ciao, -sciss-



Am 05.02.2010 um 13:13 schrieb Xiaobo Yang:

> Hi all,
>
> I want to create an xml fragment like below. Services 1 and 2 are
> retrieved from somewhere else.
>
> <services>
>  <service1>
>    <location/>
>    <time/>
>    <location/>
>    <time/>
>  </service1>
>  <service2>
>    <location/>
>    <time/>
>    <location/>
>    <time/>
>  </service2>
> </services>
>
> I hope to define a method which accepts a parameter, the service name,
> to create the xml fragment.
>
> def addService(services: java.util.List, name: String): Elem = {
>  return <{name}>
>    // loop the list to add services
>    </{name}>
> }
>
> But the above code is not recognised by scala. How to fix it?
>
> Many thanks,
> Xiaobo




--
Daniel C. Sobral

I travel to the future all the time.
Xiaobo Yang
Joined: 2010-01-28,
User offline. Last seen 42 years 45 weeks ago.
Re: xml creation issue

thx, this does the trick.

On 5 February 2010 14:37, Daniel Sobral wrote:
> I hadn't thought to use Scala 2.8's copy method. It works as a single
> expression as well:
>
> .copy(label="blah")
>
> On Fri, Feb 5, 2010 at 12:26 PM, Sciss wrote:
>>
>> i had the same problem. it seem you cannot create an XML literal where the
>> outer scope element has a variable {} name. as a workaround you could use an
>> attribute though:
>>
>> def createXML( name: String ) =
>>        ...
>>
>>
>> other way is to modify the XML literal:
>>
>> scala> def standardXML =
>>     | 1
>>     |
>> standardXML: scala.xml.Elem
>>
>> scala> def namedXML( name: String ) = {
>>     | val std = standardXML
>>     | std.copy( label = name )
>>     | }
>> namedXML: (name: String)scala.xml.Elem
>>
>> scala> namedXML( "Hallo" )
>> res0: scala.xml.Elem =
>>
>> 1
>>
>>
>> ciao, -sciss-
>>
>>
>>
>> Am 05.02.2010 um 13:13 schrieb Xiaobo Yang:
>>
>> > Hi all,
>> >
>> > I want to create an xml fragment like below. Services 1 and 2 are
>> > retrieved from somewhere else.
>> >
>> >
>> >  
>> >    
>> >    
>> >    
>> >    
>> >  
>> >  
>> >    
>> >    
>> >    
>> >    
>> >  
>> >
>> >
>> > I hope to define a method which accepts a parameter, the service name,
>> > to create the xml fragment.
>> >
>> > def addService(services: java.util.List, name: String): Elem = {
>> >  return <{name}>
>> >    // loop the list to add services
>> >    
>> > }
>> >
>> > But the above code is not recognised by scala. How to fix it?
>> >
>> > Many thanks,
>> > Xiaobo
>>
>
>
>
> --
> Daniel C. Sobral
>
> I travel to the future all the time.
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland