I am trying to parse an XML I am receiving in the ...
# suitescript
n
I am trying to parse an XML I am receiving in the response. I have parsed up until I am receiving the below. How can I parse it further to get CustomerID and Invoice Number, for example?
<Payment>
<CustomerId>SP0115</CustomerId>
<SubCustomerId>S01743</SubCustomerId>
<DivisionId>001</DivisionId>
<InvoiceNumber>112008158</InvoiceNumber>
<InvoiceInternalId>bc0c2e4b-796b-4350-b39c-dfb2bbb1f590</InvoiceInternalId>
<InvoiceDate>2013-01-21</InvoiceDate>
<InvoiceDueDate>2013-02-20</InvoiceDueDate>
<PoNum>&nbsp;</PoNum>
<InvoiceAmount>-2346.2200</InvoiceAmount>
<AmountDue>-3013.7200</AmountDue>
<AuthCode>273046</AuthCode>
<RefNum>62158562</RefNum>
<Last4>1111</Last4>
<PaymentMethod>Visa</PaymentMethod>
<DatePaid>2015-02-25T05:34:04</DatePaid>
<PaidAmount>0.2200</PaidAmount>
<PaymentInternalId>818db1ea-95b3-4b6d-a7a7-3029842c48ac</PaymentInternalId>
</Payment>
b
if you like a primitive DOM mode, use Parser.fromString
alternatively combine it with xpaths using XPath.select
i personally like neither and just hate xml in suitescript
💯 1
n
I am using XPath.select to get to this node. However, there are 3 nodes of type Payment so it returning an array.
Copy code
var path = "s:Envelope/s:Body/*[name()='GetPaymentsResponse']/*[name()='GetPaymentsResult']/*";
var payments = nsXML.XPath.select({
   node: xmlDocument,
   xpath: path + "[name()='Payment']"
});
b
add more predicates
probably using an
and
n
Copy code
payments.forEach(function (payment) {
   var res = nsXML.XPath.select({
      node: payment,
      xpath: '//CustomerId'
   });
   log.debug('Res', res);
})
It is showing me [] in log
b
my guess is that you have namespaces in there
n
there is no namespace. XML is shared above.
Copy code
var path = "s:Envelope/s:Body/*[name()='GetPaymentsResponse']/*[name()='GetPaymentsResult']/*";
var payments = nsXML.XPath.select({
   node: xmlDocument,
   xpath: path + "[name()='Payment']"
});
This works fine and gives me an array in "payments"
b
Copy code
s:Envelope
whats the s namespace
n
But afterwards it is like this:
<Payment>
<CustomerId>SP0115</CustomerId>
<SubCustomerId>S01743</SubCustomerId>
<DivisionId>001</DivisionId>
<InvoiceNumber>112008158</InvoiceNumber>
<InvoiceInternalId>bc0c2e4b-796b-4350-b39c-dfb2bbb1f590</InvoiceInternalId>
<InvoiceDate>2013-01-21</InvoiceDate>
<InvoiceDueDate>2013-02-20</InvoiceDueDate>
<PoNum>&nbsp;</PoNum>
<InvoiceAmount>-2346.2200</InvoiceAmount>
<AmountDue>-3013.7200</AmountDue>
<AuthCode>273046</AuthCode>
<RefNum>62158562</RefNum>
<Last4>1111</Last4>
<PaymentMethod>Visa</PaymentMethod>
<DatePaid>2015-02-25T05:34:04</DatePaid>
<PaidAmount>0.2200</PaidAmount>
<PaymentInternalId>818db1ea-95b3-4b6d-a7a7-3029842c48ac</PaymentInternalId>
</Payment>
b
just because there is no explicit namespace specified doesnt mean there isnt a default namespace
n
Okay, let me try that
Copy code
var res = nsXML.XPath.select({
   node: payment,
   xpath: "//s:CustomerId"
});
Empty array.
Any XML parser that you use?
should be a bunch of xpath namespaces results in google too
s
I use JSONIX