I have a xml as follows:
<Head>
<Company>
<Props>
</Props>
<Config>
</Config>
<Products>
<Product type="Random" name="xyz">
<Property name="asd" value="asd"/>
</Product>
<Product type="Random1" name="xyz1">
<Property name="asd1" value="asd1"/>
</Product>
<Product type="Random2" name="xyz">
<Property name="asd2" value="asd2"/>
</Product>
</Products>
</Company>
</Head>
I need to extract node "Product" and its attribute "name". But (Subnode of "Product") "Property" also has attribute "name". I wrote a code as follows:
val xml = XML.loadFile("product.xml")
val names = (Head \\ Company \\ prodcuts \\ product \\ "@name").map { _.text }
But this returns a list of name of product and property both. How do i select only Product names ? Thanks
You can do smth like that:
( xml \\ "Product").map( n => n \@ "name").foreach(println)
Using double backslash \\
means that you want to select all sequence elements and all it subsequence's.
You can use single backslash \
to chose only sequence element. But it works only if you have one child element but not a list.
My solution is to iterate through all Product
elements and pick up relevant sequence attribute of it.