简体   繁体   中英

Parsing through an xml

I'm trying to parse through an xml file in adwords :

<items xmlns:c="http:/example.com" xmlns:g="example.com">
  <item>
    <g:id>123</g:id>
    <g:title>
      <MYNAME]>
    </g:title>
  </item>

I'm using this bit of code

    var document2= XmlService.parse(xml);

    var root = document2.getRootElement();

    var Elements= root.getChildren();

    for (var j = 1; j < Elements.length; j++) {

       Logger.log (Elements[j].getChild('g:id');
    }  

But the error return a null as opposed to 'g:id' What should I do?

I'm using XmlService from Apps Script, specifically the method getChild(name, namespace) as follows:

/* CODE FOR DEMONSTRATION PURPOSES */
function testXML() {
  var xml = '<items xmlns:c="http://example.com" xmlns:g="example.com"><item><g:id>123</g:id><g:title>MYNAME</g:title></item></items>';
  var g_namespace = XmlService.getNamespace('g', 'example.com');
  var document = XmlService.parse(xml);
  var Elements = document.getRootElement().getChildren();
  for (var element = 0, len = Elements.length; element < len; element++) {
    Logger.log(Elements[element].getChild('id', g_namespace).getValue());
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM