Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse XML using Scala with colon (:) inside XML tag

Tags:

xml

scala

I am stuck in parsing XML which has ":" in its element name.

Sample XML is shown below:

val xml:String = <epp xmlns='urn:ietf:params:xml:ns:epp-1.0'
                 xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
                 xsi:schemaLocation='urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd'>
                 <command>
                     <create>
                         <host:create xmlns:host='urn:ietf:params:xml:ns:host-1.0'
                               xsi:schemaLocation='urn:ietf:params:xml:ns:host-1.0host-1.0.xsd'>
                             <host:name>ns-1.dns.net</host:name>
                         </host:create>
                     </create>
                     <clTRID>TRID-1-100043434343</clTRID>
                 </command>
                 </epp>

val dom = scala.xml.XML.loadString(xml)
val name = dom \\ "host:name"

name always is empty.

I need to get the value of <host:name> element tag. Please let me know how to get it.

like image 244
pawank Avatar asked May 19 '11 12:05

pawank


1 Answers

The \\ operator is misleading. It doesn't accept XPath queries, just node names. If you want to filter on the prefix name - the part before the : - that part of the query will have to be in scala. Something like this should work:

val name = (dom \\ "name").filter(_.prefix == "host")
like image 191
sblundy Avatar answered Sep 21 '22 02:09

sblundy