Extracting element names and property values from XML
I’m rel开发者_开发百科atively new to working with Scala having recently come from a java background.
I’m seeking advice on creating an efficient s means of reading an XML file and extracting element names and properties contained within where the data is enclosed between parentheses.
For example
< some name >{property}< some name/ >
So the key is where there is data contained within {}
I then want to populate a hash map with the actual element names and property values contained between {}.
I’m sure this is not overly complex but given my limited Scala expertise at this time I’d like to bounce this question off the expertise within the forum.
Many thanks to anyone taking time to answer.
[UPDATED] Just realized that you've asked about braces in text, not about xml-related syntax braces. The possible answer is:
scala> val xml = <a>
<prop>{{key1}}</prop>
<prop>{{key2}}</prop>
<prop>notkey</prop>
</a>
xml: scala.xml.Elem = <a><prop>{key1}</prop><prop>{key2}</prop><prop>notkey</prop></a>
I've doubled {
and }
to escape braces, because Scala treat {
variable}
as a substitute:
scala> val text="all your base are belong to us"
text: java.lang.String = all your base are belong to us
scala> val template = <a>{text}</a>
template: scala.xml.Elem = <a>all your base are belong to us</a>
Now back to work.
To take only prop's
use \
projection operator, which takes subnodes with a given name. To take all sub-sub-...-sub nodes in xml tree use instead \\
.
scala> val props = xml\"prop"
props: scala.xml.NodeSeq = NodeSeq(<prop>{key1}</prop>, <prop>{key2}</prop>, <prop>notkey</prop>)
val keys = props.filter (p => p.text.startsWith("{"))
res3: scala.xml.NodeSeq = NodeSeq(<prop>{key1}</prop>, <prop>{key2}</prop>)
Actually, i'm cheated here, and supposed that non-desired property field cannot exist in form {notkey. You can rewrite code to filter keys in another manner (e.g. using regexp's)
scala> for(k <- keys) {
| println(k.label+":"+k.text)
| }
prop:{key1}
prop:{key2}
精彩评论