scala - Extracting element names and property values from XML -
i’m relatively new working scala having come java background.
i’m seeking advice on creating efficient s means of reading xml file , extracting element names , properties contained within data enclosed between parentheses.
for example
< name >{property}< name/ >
so key there data contained within {}
i want populate hash map actual element names , property values contained between {}.
i’m sure not overly complex given limited scala expertise @ time i’d bounce question off expertise within forum.
many taking time answer.
[updated] realized you've asked braces in text, not xml-related syntax braces. 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 {
, }
escape braces, because scala treat {
variable}
substitute:
scala> val text="all base belong us" text: java.lang.string = base belong scala> val template = <a>{text}</a> template: scala.xml.elem = <a>all base belong us</a>
now work.
take prop's
use \
projection operator, takes subnodes given name. take 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, , supposed non-desired property field cannot exist in form {notkey. can rewrite code filter keys in manner (e.g. using regexp's)
scala> for(k <- keys) { | println(k.label+":"+k.text) | } prop:{key1} prop:{key2}
| om-nom-nom, many this, it’s type of guidance , example i’d been hoping for. gary – gary jul 21 '11 @ 19:32 | ||
| @gary, why don't accept answer then? ;) – om-nom-nom jul 21 '11 @ 19:51 | ||
| sorry, i'd forgotten check accepted. again. – gary jul 21 '11 @ 21:02 |