Re: repost -Difficulties with xml
- To: mathgroup at smc.vnet.net
- Subject: [mg69329] Re: repost -Difficulties with xml
- From: albert <awnl at arcor.de>
- Date: Thu, 7 Sep 2006 04:30:22 -0400 (EDT)
- References: <edm1b4$co5$1@smc.vnet.net>
Pascal Mercier wrote: > I am having problems accessing and changing xml attributes/properties > after importing an xml file and I'm looking for some help. > > z=Import["xmltest.xml","XML"]; > > Could anybody give me an hint about the correct syntax to use to be be > able to directly access and change the value of the freq attribute of > peakID2 of ensemble2 for instance? I just can't seem to be able to find it > after reading Mathematica's documentation. AFAIK the idea is to use the usual pattern matching abilities of mathematica to work with the resultring symbolic XML. Unfortunatly the patterns will become somewhat complicated, but with a little practice you will get the hang. In your case to extract the value you specified could be done with something like: Cases[z, XMLElement["ensemble", {"ensemble" -> 2}, {h1___, XMLElement["peak", {"peakID" -> 2}, {h2___, XMLElement["freq", {}, {f_}], t2___}], t1___}] :> f, Infinity ] to create a symbolic-XML-expression with the specified value changed to e.g. 10 you could use: newfreq = 10; ReplaceAll[z, XMLElement["ensemble", {"ensemble" -> 2}, {h1___, XMLElement["peak", {"peakID" -> 2}, {h2___, XMLElement["freq", {}, {f_}], t2___}], t1___}] :> XMLElement["ensemble", {"ensemble" -> 2}, {h1, XMLElement["peak", {"peakID" -> 2},{h2, XMLElement["freq", {}, {newfreq}], t2}], t1}] ] I have only tested this for your simple example and am sure it can be improved. Also note that I have enclosed all tag and attribute names with "", because Import generates them as strings, not symbols. I guess you copied your example from an output-cell, where the "" are not shown. To make life easier for the ones who want to help you should post in InputForm only, even if you are copying contents of Output cells. To do this select the cell and klick Cell -> Convert To -> InputForm. The resulting cell can usually be copied to any email client without any formatting problems. hth albert