MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: RuleDelayed for parsing XML with multiple children

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109611] Re: RuleDelayed for parsing XML with multiple children
  • From: "Hans Michel" <hmichel at cox.net>
  • Date: Sat, 8 May 2010 07:06:22 -0400 (EDT)
  • References: <hs0plr$doj$1@smc.vnet.net>

Try:
 
xmlTree =
 ImportString["<LevelA><LevelB><Child>value1</Child></LevelB><LevelB><Child>value2</Child></LevelB></LevelA>","XML"]Cases[xmlTree, XMLElement["Child", _, {whatyouwant_}] -> whatyouwant, Infinity]Not really intuitive. I think WRI needs to have some standard XML parsingfunctions. XSLT and XPath have been around a while, so has Mathematica andall those algorithms that xslt parsers are based on (recursion). But theXMLElement and cases... approach is not intuitive or "easy to grasp".mma sessionIn[1]:=xmlTree=ImportString["<LevelA><LevelB><Child>value1</Child></LevelB><LevelB><Child>value2</Child></LevelB></LevelA>","XML"]Out[1]=XMLObject[Document][{},XMLElement[LevelA,{},{XMLElement[LevelB,{},{XMLElement[Child,{},{value1}]}],XMLElement[LevelB,{},{XMLElement[Child,{},{value2}]}]}],{}]In[2]:=Cases[xmlTree,XMLElement["Child",_,{whatyouwant_}]->whatyouwant,Infinity]Out[2]= {value1,value2}Hans"Zach Bjornson" <bjornson at mit.edu> wrote in messagenews:hs0plr$doj$1 at smc.vnet.net...> Hi,>> I'
 m trying to extract multiple values (same depth, different physical> level) from an XML tree using RuleDelayed, but I realized that using> RuleDelayed only extracts one of the children. That is:>> XmlTree follows the form:> <LevelA>> <LevelB>> <Child>value1</Child>> </LevelB>> <LevelB>> <Child>value2</Child>> </LevelB>> </LevelA>>> I want those two values.>> Cases[XmlTree,>XMLElement["LevelA",_,{___,XMLElement["LevelB",_,{___,XMLElement["Child",_,{WantThisValue_}],___}],___}]:>{WantThisValue},Infinity]>> This only gives value1. I can explicitly add more XMLElement tags to get> the other value>> Cases[XmlTree,>XMLElement["LevelA",_,{___,XMLElement["LevelB",_,{___,XMLElement["Child",_,{WantThisValue_}],___}],___,XMLElement["LevelB",_,{___,XMLElement["Child",_,{WantThisValueToo_}],___}]:>{WantThisValue,WantThisValueToo},Infinity]>> but I don't want to use an explicit structure because the number of> children/values varies.>> Any have suggestions for the best alternative method 
 to the RuleDelayed> syntax I'm using? Changing the innermost XMLElement tag to simply> XMLElement["Child",_,_] gives the proper answer, albeit with the messy> flanking tree structure. Dropping indices is not an ideal solution.>> Thanks!> Zach>



  • Prev by Date: Re: Easy question, please help to run a function n times
  • Next by Date: Re: RuleDelayed for parsing XML with multiple children
  • Previous by thread: RuleDelayed for parsing XML with multiple children
  • Next by thread: Re: RuleDelayed for parsing XML with multiple children