Node.js get all paths in XML or JSON

Viewed 26

I have a large XML file of 4000+ lines with 10 child nodes deep I need a way to show the path of each line of the file. I can convert the XML to JSON if that is more efficient or easier.

const fs = require('fs')
const xml2js = require('xml2js')
const xpath = require('xpath')
const dom = require('xmldom').DOMParser
const parser = new xml2js.Parser()

const path = 'sample.xml'

fs.readFile(path, "utf-8", (err, data) => {
  parser.parseString(data, (err, res) => {
    if(err) {
        console.log('Err');
        console.log(err);
    } else {
        console.log(data);
        const doc = new dom().parseFromString(data);
        const nodes = xpath.select("Appointment", doc);
        doc.forEach((n,i) => {
            console.log(n);
    })
    }            
  });
})

Here is an example of the XML file use, this is just the top bit of it.

    <ArrayOfAppointment xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/DealerBuilt.BaseApi">
  <Appointment>
    <Placement>
      <GroupId>10</GroupId>
      <GroupName>sample string 11</GroupName>
      <EnvironmentId>9</EnvironmentId>
      <EnvironmentType>Undefined</EnvironmentType>
      <DealerId>8</DealerId>
      <SourceId>7</SourceId>
      <CompanyId>5</CompanyId>
      <CompanyName>sample string 6</CompanyName>
      <StoreId>3</StoreId>
      <StoreName>sample string 4</StoreName>
      <ServiceLocationId>1</ServiceLocationId>
      <ServiceLocationName>sample string 2</ServiceLocationName>
    </Placement>
    <AppointmentKey>sample string 1</AppointmentKey>
    <Attributes xmlns:d3p1="http://schemas.datacontract.org/2004/07/DealerBuilt.Models.Appointments">
      <d3p1:AppointmentMethod>sample string 8</d3p1:AppointmentMethod>
      <d3p1:AppointmentNumber>1</d3p1:AppointmentNumber>
      <d3p1:AppointmentScenario>None</d3p1:AppointmentScenario>
      <d3p1:AppointmentTime>2022-08-30T18:02:48.9916918Z</d3p1:AppointmentTime>
      <d3p1:Comment>sample string 4</d3p1:Comment>
      <d3p1:Concerns>
        <d3p1:Concern>
          <d3p1:CampaignDispositionCode>sample string 8</d3p1:CampaignDispositionCode>
          <d3p1:CampaignNumber>sample string 7</d3p1:CampaignNumber>
          <d3p1:CauseDescription>sample string 10</d3p1:CauseDescription>
          <d3p1:Correction>sample string 9</d3p1:Correction>
          <d3p1:CustomerStatement>sample string 2</d3p1:CustomerStatement>
          <d3p1:DepartmentDescription>sample string 6</d3p1:DepartmentDescription>
          <d3p1:DepartmentId>5</d3p1:DepartmentId>
          <d3p1:JobCode>sample string 3</d3p1:JobCode>
          <d3p1:MinutesEstimated>4</d3p1:MinutesEstimated>
          <d3p1:PayType>Unknown</d3p1:PayType>
          <d3p1:Seq>11</d3p1:Seq>
        </d3p1:Concern>
        <d3p1:Concern>
          <d3p1:CampaignDispositionCode>sample string 8</d3p1:CampaignDispositionCode>
          <d3p1:CampaignNumber>sample string 7</d3p1:CampaignNumber>
          <d3p1:CauseDescription>sample string 10</d3p1:CauseDescription>
          <d3p1:Correction>sample string 9</d3p1:Correction>
          <d3p1:CustomerStatement>sample string 2</d3p1:CustomerStatement>
          <d3p1:DepartmentDescription>sample string 6</d3p1:DepartmentDescription>
          <d3p1:DepartmentId>5</d3p1:DepartmentId>
          <d3p1:JobCode>sample string 3</d3p1:JobCode>
          <d3p1:MinutesEstimated>4</d3p1:MinutesEstimated>
          <d3p1:PayType>Unknown</d3p1:PayType>
          <d3p1:Seq>11</d3p1:Seq>
        </d3p1:Concern>
      </d3p1:Concerns>

Here is an example of the results that I am looking for that will match with the XML line per line.

/
/Appointment
/Appointment/Placement
/Appointment/Placement
/Appointment/Placement
/Appointment/Placement
/Appointment/Placement
/Appointment/Placement
/Appointment/Placement
/Appointment/Placement
/Appointment/Placement
/Appointment/Placement
/Appointment/Placement
/Appointment/Placement
/Appointment/Placement
/Appointment/Placement
/Appointment
/Appointment
/Appointment/Attributes
/Appointment/Attributes
/Appointment/Attributes
/Appointment/Attributes
/Appointment/Attributes
/Appointment/Attributes/Concerns
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns/Concern
/Appointment/Attributes/Concerns
0 Answers
Related