'How to extract all datas from xml DOM in node JS

I am going to extract all attributes and texts from xml DOM in express Now I am using xml-query node module

    <?xml version="1.0"?>
    <OrderMessageBatch batchNumber="3012566157">
    <hubOrder transactionID="3001372018">
      <lineItem>
        <lineItemId>3001389044</lineItemId>
        <orderLineNumber>1</orderLineNumber>
        <merchantLineNumber>1</merchantLineNumber>
        <qtyOrdered>3</qtyOrdered>
        <unitOfMeasure>EA</unitOfMeasure>
        <UPC>9999999990101</UPC>
        <poLineData />
      </lineItem>
      <personPlace personPlaceID="PP3012385024">
        <name1>John Doe</name1>
        <address1>1 Fuller Road</address1>
        <city>Albany</city>
        <state>NY</state>
        <postalCode>12203</postalCode>
        <dayPhone>555-555-5555</dayPhone>
      </personPlace>
      <personPlace personPlaceID="PP3012385026">
        <name1>Mary Smith</name1>
        <address1>123 THD Lane</address1>
        <city>Atlanta</city>
        <state>GA</state>
        <postalCode>30339</postalCode>
        <email>[email protected]</email>
        <partnerPersonPlaceId>8119</partnerPersonPlaceId>
      </personPlace>
      <personPlace personPlaceID="PP3012385025">
        <name1>Home Depot</name1>
        <partnerPersonPlaceId>8119</partnerPersonPlaceId>
      </personPlace>
    </hubOrder>
<hubOrder transactionID="3001372018">
      <lineItem>
        <lineItemId>3001389044</lineItemId>
        <orderLineNumber>1</orderLineNumber>
        <merchantLineNumber>1</merchantLineNumber>
        <qtyOrdered>3</qtyOrdered>
        <unitOfMeasure>EA</unitOfMeasure>
        <UPC>9999999990101</UPC>
        <poLineData />
      </lineItem>
      <personPlace personPlaceID="PP3012385024">
        <name1>John Doe</name1>
        <address1>1 Fuller Road</address1>
        <city>Albany</city>
        <state>NY</state>
        <postalCode>12203</postalCode>
        <dayPhone>555-555-5555</dayPhone>
      </personPlace>
      <personPlace personPlaceID="PP3012385026">
        <name1>Mary Smith</name1>
        <address1>123 THD Lane</address1>
        <city>Atlanta</city>
        <state>GA</state>
        <postalCode>30339</postalCode>
        <email>[email protected]</email>
        <partnerPersonPlaceId>8119</partnerPersonPlaceId>
      </personPlace>
      <personPlace personPlaceID="PP3012385025">
        <name1>Home Depot</name1>
        <partnerPersonPlaceId>8119</partnerPersonPlaceId>
      </personPlace>
    </hubOrder>

As you can see there are several hubOrder attributes in xml file and and each hubOrder attribute have many personPlace attributes. Now xmlDOM.find('orderLineNumber').text() lists texts not regarding to xml structrure Is there any node module to extract all neccessary datas? Or what can I do on with this module?



Solution 1:[1]

Use the .map function to convert the result of .find to an array, for example:

xmlQuery(xmlDOM).find("hubOrder").map(
  hubOrder => xmlQuery(hubOrder).find("lineItem").map(
    lineItem => xmlQuery(lineItem).find("orderLineNumber").text()
  )
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Heiko Theißen