Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have seen and tried many different versions of XML data parsing here on this forum, and even some in the documentation. Is there a tried and true "Best Practice" way to extract all the XMLElement values or is each new file type a hack a thon to extract the correct data and fields? Here's the code I'm using now, but it's not efficient and does not grab all the values needed. Is there a better way?

a=thePartIreallyneeded;
elements = Column@Cases[dat, 
XMLElement[tag : _, _, value : _] :> (tag -> value), Infinity];
pairsE = List @@ elements;
gatheredE = GatherBy[pairsE, #[[1]] &];
propertiesE = Part[#, 1, 1] & /@ gatheredE;
targetPositionsE = gatheredE[[a]];

Here is a very small snippet of the section of values I'd like to extract. What I need to get are the Color Names, the Target Id values and the values for each color name. Thank you!

XMLElement[{"http://colorexchangeformat.com/CxF3-core", "Resources"}, {}, \
{XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
"ObjectCollection"}, {}, \
{XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
  "Object"}, {"ObjectType" -> "Target", "Name" -> "Target1", 
  "Id" -> "c1"}, \
{XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
    "CreationDate"}, {}, {"2013-05-17T11:59:45-06:00"}], 
  XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
    "DeviceColorValues"}, {}, \
{XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
      "ColorCMYK"}, {"ColorSpecification" -> 
       "Unknown"}, \
{XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
        "Cyan"}, {}, {"0"}], 
      XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
        "Magenta"}, {}, {"0"}], 
      XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
        "Yellow"}, {}, {"0"}], 
      XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
        "Black"}, {}, {"0"}]}]}]}],
 XMLElement[{"http://colorexchangeformat.com/CxF3-core", "Object"}, {"ObjectType" ->   "Target", "Name" -> "Target11", "Id" -> "c11"}, \
{XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
"CreationDate"}, {}, {"2013-05-17T11:59:45-06:00"}], 
 XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
"DeviceColorValues"}, {}, \
{XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
  "ColorCMYK"}, {"ColorSpecification" -> 
   "Unknown"}, \
{XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
    "Cyan"}, {}, {"11.596052607559"}], 
  XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
    "Magenta"}, {}, {"25.435485248175"}], 
  XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
    "Yellow"}, {}, {"0"}], 
  XMLElement[{"http://colorexchangeformat.com/CxF3-core", 
    "Black"}, {}, {"0"}]}]}]}]}]}]
share|improve this question
1  
Show us some sample of your XML, and what part do you want to extract. –  Murta Jun 2 '13 at 20:10
    
Related / possible duplicate: mathematica.stackexchange.com/q/3111/121 (Let's see some sample data first.) –  Mr.Wizard Jun 2 '13 at 20:36
    
@Mr.Wizard That's about JSON. Does its handling sufficiently resemble that of XML to call this question a duplicate? –  Sjoerd C. de Vries Jun 2 '13 at 20:55
    
@Sjoerd If it's imported as rules I think perhaps so, though I fully admit I may not be thinking clearly regarding the problem. I'm still waiting for some sample data. –  Mr.Wizard Jun 2 '13 at 21:06
1  
@Mr.Wizard It doesn't. Try this: Import["http://www.w3schools.com/xml/simple.xml"] –  Sjoerd C. de Vries Jun 2 '13 at 21:12

1 Answer 1

up vote 4 down vote accepted

This is one way. Using data=xmlInfo

nodes=Cases[data,XMLElement[{_,"Object"},{"ObjectType"->"Target","Name"->target_,"Id"->id_},{x__}]:> {target,id,{x}} ,\[Infinity]];
getColors=Cases[#,XMLElement[{_,color_/;MemberQ[{"Cyan","Yellow","Black","Magenta"},color]},{},{value_}]-> {color,value},\[Infinity]]&;
nodes[[All,3]]=getColors/@nodes[[All,3]];
nodes

{{Target1,c1,{{Cyan,0},{Magenta,0},{Yellow,0},{Black,0}}},{Target11,c11,{{Cyan,11.596052607559},{Magenta,25.435485248175},{Yellow,0},{Black,0}}}}

I really I'm not confortable in parsing XML like this. I fell it's clumsy, but it's they the only way I know until now.

share|improve this answer
    
Thanks! Do you know how can the target values be saved as a separate list and the color values also be saved as a separate list? –  R Hall Jun 2 '13 at 22:43
1  
Just use nodes[[All, 1]] or nodes[[All, -1]] to get it. –  Murta Jun 2 '13 at 22:44
    
Rockin! Thanks! –  R Hall Jun 2 '13 at 22:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.