André Aulich
André Aulich

Reputation: 190

extract value from XML in pure JavaScript

I am fetching an XML response from an external tool using

var myXML = api.path("item/"+itemId+"/metadata").dataType("xml").get();

The XML structure I receive this way looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MetadataListDocument xmlns="http://xml.vidispine.com/schema/vidispine">
  <item id="VX-3594">
    <metadata>
      <revision>VX-61522,VX-61520,VX-61519,VX-61515,VX-61513,VX-61514,VX-61525,VX-61523</revision>
      <group>Final Cut Server</group>
      <timespan start="-INF" end="+INF">
        <field uuid="2913190d-635c-4f81-951c-4ff3f934a547" user="system" timestamp="2015-03-16T16:52:48.061+01:00" change="VX-61523">
          <name>shapeTag</name>
          <value uuid="2c8b57d0-5c47-49c9-a4f0-54f7fef803cc" user="system" timestamp="2015-03-16T16:52:48.061+01:00" change="VX-61523">lowres</value>
          <value uuid="6d994b0c-9d91-4b32-8052-824b3467a29e" user="system" timestamp="2015-03-16T16:52:48.061+01:00" change="VX-61523">original</value>
          <value uuid="85cd07b7-6f13-4f6c-86fa-e42db20860c0" user="system" timestamp="2015-03-16T16:52:48.061+01:00" change="VX-61523">house_format_audio_through</value>
        </field>
        <field uuid="e2b4ca3c-6765-4890-99f4-f6e0624099eb" user="admin" timestamp="2015-03-16T16:47:57.506+01:00" change="VX-61513">
          <name>portal_mf129489</name>
          <value uuid="ab60ed17-75f4-4593-b068-9d8b6a700025" user="admin" timestamp="2015-03-16T16:47:57.506+01:00" change="VX-61513">no</value>
        </field>
      </timespan>
    </metadata>
  </item>
</MetadataListDocument>

Now I need to extract the value of the field with the name portal_mf129489, which in this case would be no.

To extract data from the xml output I already set the default name space:

default xml namespace = "http://xml.vidispine.com/schema/vidispine";

and I can extract things using

 var myTimespan = myXML.item.metadata.timespan;

My question though is, how can I find a field element, which has a child element named portal_mf129489? If I find this element, I would need to fetch the value of the field's child element named value.

Each XML has multiple field elements, and inside the field I am looking for, name is always the first child element, value is always the second element.

Any ideas?

Upvotes: 1

Views: 1511

Answers (2)

Andr&#233; Aulich
Andr&#233; Aulich

Reputation: 190

Okay, got it working, now. I added the function:

function getField(ts, name) {
  for (var i = 0; i < ts.field.length(); i++) {
    var f = ts.field[i];
    if (f.name == name) {
      return f;
    }
  }
}

and this:

var r = api.path("item/"+itemId+"/metadata").dataType("xml").get();
var metadata = r.item[0].metadata;
var ts = metadata.timespan[0];
var tag = getField(ts, 'portal_mf129489');
if (tag && tag.value) {
  logger.log('value: '+tag.value[0]);
}

Upvotes: 0

Zeljko
Zeljko

Reputation: 41

You can do this with little help of jQuery. The idea is to use jQuery to query XML document:

var xml = $.parseXML('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><MetadataListDocument xmlns="http://xml.vidispine.com/schema/vidispine"><item id="VX-3594"><metadata><revision>VX-61522,VX-61520,VX-61519,VX-61515,VX-61513,VX-61514,VX-61525,VX-61523</revision><group>Final Cut Server</group><timespan start="-INF" end="+INF"><field uuid="2913190d-635c-4f81-951c-4ff3f934a547" user="system" timestamp="2015-03-16T16:52:48.061+01:00" change="VX-61523"><name>shapeTag</name><value uuid="2c8b57d0-5c47-49c9-a4f0-54f7fef803cc" user="system" timestamp="2015-03-16T16:52:48.061+01:00" change="VX-61523">lowres</value><value uuid="6d994b0c-9d91-4b32-8052-824b3467a29e" user="system" timestamp="2015-03-16T16:52:48.061+01:00" change="VX-61523">original</value><value uuid="85cd07b7-6f13-4f6c-86fa-e42db20860c0" user="system" timestamp="2015-03-16T16:52:48.061+01:00" change="VX-61523">house_format_audio_through</value></field><field uuid="e2b4ca3c-6765-4890-99f4-f6e0624099eb" user="admin" timestamp="2015-03-16T16:47:57.506+01:00" change="VX-61513"><name>portal_mf129489</name><value uuid="ab60ed17-75f4-4593-b068-9d8b6a700025" user="admin" timestamp="2015-03-16T16:47:57.506+01:00" change="VX-61513">no</value></field></timespan></metadata></item></MetadataListDocument>');

var value = $(xml).find('field > name').filter(function(){ return $(this).text() === 'portal_mf129489' }).siblings('value').text();

$('body').append(value);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Related Questions