Message Boards Message Boards

0
|
6851 Views
|
5 Replies
|
4 Total Likes
View groups...
Share
Share this post:
GROUPS:

How Do I Extract a Specific Value from an XMLElement[]?

Posted 9 years ago

After a series of Cases statements on in imported XML file, I have many XMLElements, one of which looks like this...

el = XMLElement[
   "price", {"id" -> "12", "selid" -> "865986582", 
    "marketid" -> "258568238", "auxids" -> "", "price" -> "11/8", 
    "pricedec" -> "2.38", "cid" -> "7x12", "movement" -> "O", 
    "bslink" -> "Y", "date_stamp" -> "2015-04-25 11:50:21", 
    "opprice" -> "", "oppriced" -> ""}, {}];

I want to extract the attributes individually, for example, pricedec.

I can do this...

el[[2, 6, 2]]

...but it is not robust, as some elements are not present in various versions of the XMLElement returned. So the index 6 is not reliable.

I could write my own function looking for Keys[] and Values[], but I assume a better way must already exist, as what I am trying to accomplish is such a common thing.

Any thoughts?

POSTED BY: Brad Varey
5 Replies
Posted 9 years ago

And of course for a list of such elements you could extract a list of the items:

In[1]:= el = 
  XMLElement[
   "price", {"id" -> "12", "selid" -> "865986582", 
    "marketid" -> "258568238", "auxids" -> "", "price" -> "11/8", 
    "pricedec" -> "2.38", "cid" -> "7x12", "movement" -> "O", 
    "bslink" -> "Y", "date_stamp" -> "2015-04-25 11:50:21", 
    "opprice" -> "", "oppriced" -> ""}, {}];

In[2]:= el2 = 
  XMLElement[
   "price", {"id" -> "13", "selid" -> "865986582", 
    "marketid" -> "258568238", "auxids" -> "", "price" -> "12/8", 
    "pricedec" -> "2.38", "cid" -> "7x12", "movement" -> "O", 
    "bslink" -> "Y", "date_stamp" -> "2015-04-25 11:50:21", 
    "opprice" -> "", "oppriced" -> ""}, {}];

In[3]:= {"id", "price"} /. {el, el2}[[All, 2]]

Out[3]= {{"12", "11/8"}, {"13", "12/8"}}

You could also quite easily build a list of Associations so that you could extract data by Keys.

In[4]:= data = Association /@ {el, el2}[[All, 2]]

Out[4]= {<|"id" -> "12", "selid" -> "865986582", 
  "marketid" -> "258568238", "auxids" -> "", "price" -> "11/8", 
  "pricedec" -> "2.38", "cid" -> "7x12", "movement" -> "O", 
  "bslink" -> "Y", "date_stamp" -> "2015-04-25 11:50:21", 
  "opprice" -> "", "oppriced" -> ""|>, <|"id" -> "13", 
  "selid" -> "865986582", "marketid" -> "258568238", "auxids" -> "", 
  "price" -> "12/8", "pricedec" -> "2.38", "cid" -> "7x12", 
  "movement" -> "O", "bslink" -> "Y", 
  "date_stamp" -> "2015-04-25 11:50:21", "opprice" -> "", 
  "oppriced" -> ""|>}

In[5]:= data[[All, {"id", "price"}]]

Out[5]= {<|"id" -> "12", "price" -> "11/8"|>, <|"id" -> "13", 
  "price" -> "12/8"|>}

In[6]:= Query[Select[#price == "11/8" &]]@data

Out[6]= {<|"id" -> "12", "selid" -> "865986582", 
  "marketid" -> "258568238", "auxids" -> "", "price" -> "11/8", 
  "pricedec" -> "2.38", "cid" -> "7x12", "movement" -> "O", 
  "bslink" -> "Y", "date_stamp" -> "2015-04-25 11:50:21", 
  "opprice" -> "", "oppriced" -> ""|>}

Although the syntax of the new Dataset capabilities can be a bit obscure.

POSTED BY: David Keith

I would use:

FirstCase[el, ("pricedec" -> x_ ):> x,Null,Infinity]
POSTED BY: Rodrigo Murta
Posted 9 years ago

Or, you may try:

Cases[el, {__, "pricedec" -> x_, __} :> x, Infinity][[1]]
POSTED BY: Sandu Ursu
Posted 9 years ago

Hi Henrik

Thanks.

When it is THAT simple, it is great (but embarrassing to have asked!)

Regards

Brad

POSTED BY: Brad Varey

Hi Brad,

your imported data are (as rules) already in a perfect form! You can - in a robust way - extract specific values, e.g.:

pricedec = "pricedec" /. el[[2]]

Hope this helps.

Henrik

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract