0

I need to pull this XML structure to a Google spreadsheet,

<directory>
    <fieldset>
        <field id="displayName">Display name</field>
        <field id="firstName">First name</field>
    </fieldset>
    <employees>
        <employee id="200">
            <field id="displayName">Donald Duck</field>
            <field id="firstName">Donald</field>
        </employee>
    </employees>
</directory>

I've pulled all the <field id>, but I need just the <employee id> and I don't know how to do, pardon me I haven't any knowledge on XML so please help me.

function ImportXML() {

   var url = "https://api.Fake.com/employees/directory";
   var response = UrlFetchApp.fetch(url, params);



    var xml = response.getContentText();
    var doc = XmlService.parse(xml);
    var root = doc.getRootElement();


    logChildren(doc.getRootElement().getChildren());


 function logChildren(elements){


    var ss = SpreadsheetApp.getActiveSpreadsheet();

    var sheet = ss.getSheets()[0];  

    for (var i = 0; i < elements.length; i++) {

    var range = sheet.getRange(1,i+1);

    range.setValue(elements[i].getText());

    if(elements[i].getContentSize() > 1){
    var children = elements[i].getChildren();

    logChildren(children);
    }
     }
      }

Thanks for your help.

1
  • Sorry, I need to excract the 'employee id' Commented Jul 8, 2017 at 16:23

1 Answer 1

1

Check this document regarding reading a specific node using appscript

getChild(name)

Gets the first Element node with the given name and no namespace that is an immediate child of this Element node. If there is no such node, this method returns null.

Parameters

Name Type Description name String the name of the child Element node Return

Element — the Element node, or null if there is no immediate child Element node with the given name and no namespace

Use this working example as your guide.

  function readSpecificNode() {
  var url = 'https://www.w3schools.com/xml/books.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var entries = document.getRootElement().getChild('book').getChild('title').getAttribute('lang').getValue();
  Logger.log(entries);
}

Just do not forget to change the url to your designated xml and the getChild element from 'book', 'title' and 'lang' to 'directory', 'employees', 'employee' then setting the getAttribute to 'id' so it would fit your desired output.

Sign up to request clarification or add additional context in comments.

2 Comments

var entries = document.getRootElement().getChild('directory').getChild('employees').getChild('employee').getAttribute('id').getValue();
I got :"TypeError: Cannot call method "getChild" of null. "

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.