简体   繁体   中英

Google Apps Script: Directly accessing a XML element

How do I get the element value of <PeriodLength> in the XML document below?

<AnnualPeriods>
     <FiscalPeriod Type="Annual" EndDate="2009-06-30" FiscalYear="2009">
        <Statement Type="**INC**">
             <FPHeader>
                 <PeriodLength>**12**</PeriodLength>
                 <periodType Code="M">Months</periodType>

This is my current code snippet:

for (var k = 0; k < statements.length; k++) {     
   var values = elements[i].getChildren('Statement')[k]
       .getChildren('lineItem');
   var statementType = elements[i].getChildren('Statement')[k]
       .getAttribute('Type'); // INC, BAL, CAS
   var header = elements[i].getChildren('Statement')[k]
       .getChild('FPHeader');
   var periodLength = header.getChild('PeriodLength');
   var periodType = header.getChild('periodType');
}

Instead of looping through the statements, I would like to "directly" access them by specifying the element as a "path", eg INC/PeriodLength .

How would you recommend doing this?

There is no XPath type capability in the Apps Script XML service. There some other 3rd party XML JavaScript parsers you can look into for this capability.

Some ideas here -

Cross-browser XPath implementation in JavaScript

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