- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
xml creation issue
Fri, 2010-02-05, 14:13
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
Fri, 2010-02-05, 15:47
#2
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:
--
Daniel C. Sobral
I travel to the future all the time.
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.
Fri, 2010-02-05, 16:17
#3
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.
>
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