8
$\begingroup$

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"}]}]}]}]}]}]
$\endgroup$
10
  • 1
    $\begingroup$ Show us some sample of your XML, and what part do you want to extract. $\endgroup$
    – Murta
    Jun 2, 2013 at 20:10
  • $\begingroup$ Related / possible duplicate: mathematica.stackexchange.com/q/3111/121 (Let's see some sample data first.) $\endgroup$
    – Mr.Wizard
    Jun 2, 2013 at 20:36
  • $\begingroup$ @Mr.Wizard That's about JSON. Does its handling sufficiently resemble that of XML to call this question a duplicate? $\endgroup$ Jun 2, 2013 at 20:55
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Jun 2, 2013 at 21:06
  • 1
    $\begingroup$ @Mr.Wizard It doesn't. Try this: Import["http://www.w3schools.com/xml/simple.xml"] $\endgroup$ Jun 2, 2013 at 21:12

1 Answer 1

4
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$ Jun 2, 2013 at 22:43
  • 1
    $\begingroup$ Just use nodes[[All, 1]] or nodes[[All, -1]] to get it. $\endgroup$
    – Murta
    Jun 2, 2013 at 22:44
  • $\begingroup$ Rockin! Thanks! $\endgroup$ Jun 2, 2013 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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