Parse XML with Scala with a colon (:) inside an XML tag

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

An XML example 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 empty.

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

+6
source share
1 answer

The \\ operator is misleading. It does not accept XPath requests, just node names. If you want to filter the prefix name - the part before: - this part of the request must be in scala. Something like this should work:

 val name = (dom \\ "name").filter(_.prefix == "host") 
+9
source

Source: https://habr.com/ru/post/888537/


All Articles