Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8901471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:21:49+00:00 2026-06-15T01:21:49+00:00

I am parsing xml using jquery. I attached an image that shows the sort

  • 0

I am parsing xml using jquery. I attached an image that shows the sort of xml I want to parse. As you can see that it has several tags but I need to parse following in the order as it is appeared in the image.

ROUTE
   LENGTH time dist
   WALK  time dist
   LINE  code
      STOP                   //<--- this stop means all stops in this line tag
         NAME val            //<--- val of every stop inside line tag
      STOP                   //<--- this stop means all stops in this line tag
         NAME val            //<--- val of every stop inside line tag
      STOP                   //<--- this stop means all stops in this line tag
         NAME val            //<--- val of every stop inside line tag
   LINE code
      STOP                   //<--- this stop means all stops in this line tag
         NAME val            //<--- val of every stop inside line tag
      STOP                   //<--- this stop means all stops in this line tag
         NAME val            //<--- val of every stop inside line tag
      STOP                   //<--- this stop means all stops in this line tag
         NAME val            //<--- val of every stop inside line tag
   WALK time dist

Note: there could be 1, 2 or more than 2 LINES tags.

My current code does show data but for only one i.e. first LINE and only first STOP inside that LINE. So how can I parse this xml so that I could display it as it is in XML?

Here is my code

 parseXMLForRoute = function (xmlObject) {
    var xmlObj = $(xmlObject).find('ROUTE');

    xmlObj.each(function () {
        var el          = $(this),
            startTime   = el.find('WALK').find('LENGTH').attr('time'),
            distance    = el.find('WALK').find('LENGTH').attr('dist'),
            routeTime   = el.find('LENGTH').attr('time'),
            routeDist   = el.find('LENGTH').attr('dist'),
            busNumber   = el.find('LINE').attr('code').slice(1, 4),
            busStops    = el.find('LINE').find('STOP').find('NAME').attr('val');

        container.append(createDOMElement('h4', '', '', 'Departure'))
                 .append(createDOMElement('p', '', '', from))
                 .append(createDOMElement('p', '', '', "Route Time: " + routeTime))
                 .append(createDOMElement('p', '', '', "Route Dist: " + routeDist))
                 .append(createDOMElement('section', '', '', 'WALK PIC'))
                 .append(createDOMElement('p', '', '', "Walk Time: " + startTime))
                 .append(createDOMElement('p', '', '', "Walk Dist: " + distance))
                 .append(createDOMElement('p', '', '', 'BUS PIC'))
                 .append(createDOMElement('p', '', '', "Bus Number: " + parseLineNumbers(xmlObj)))
                 .append(createDOMElement('p', '', '', "Bus Stops: " + busStops));

        //console.log($(this).find('WALK'));
    });
    console.log($(xmlObject));
},

parseLineNumbers = function (xmlObject) {
    console.log('len: ' + $(xmlObject).find('LINE').length);
    $(xmlObject).find('LINE').each(function () {
        console.log($(this).attr('code').slice(1, 4));
    });
    //console.log($(xmlObject).find('LINE').attr('code').slice(1, 4));
},

createDOMElement = function (tagName, classs, id, data) {
    return '<' + tagName + ' class="' + classs + '" id="' + id + '">' + data + '</' + tagName + '>';
}

here is xml

enter image description here

UPDATE

I want to have output something similar to below

Starting street address
WALK this amount of distance(e.g. 1 meter) for some time(e.g. 1 min)
STOP name // after walking then user reached to this stop
code // code attribute inside LINE tag which is a bus number
then display the name and times of all stops in this LINE
code // code attribute inside SECOND LINE
then display all the stops name and times in this second LINE
then walk for some distance(e.g. 1 meter) and some time(e.g. 1 min)
Destination address
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:21:50+00:00Added an answer on June 15, 2026 at 1:21 am

    You need to iterate over the bus lines and inside each bus line over the bus stops. I hope this leads you in the right directiion.

    parseXMLForRoute = function (xmlObject) {    
        var route = $(xmlObject).find('ROUTE');
        route.each(function() {
            var el = $(this);
            var startTime = el.find('WALK').find('LENGTH').attr('time');
            var distance = el.find('WALK').find('LENGTH').attr('dist');
            var routeTime = el.find('LENGTH').attr('time');
            var routeDist = el.find('LENGTH').attr('dist');
            var lines = el.find('LINE');
    
            container.append(createDOMElement('h4', '', '', 'Departure'))
                .append(createDOMElement('p', '', '', from))
                .append(createDOMElement('p', '', '', "Route Time: " + routeTime))
                .append(createDOMElement('p', '', '', "Route Dist: " + routeDist))
                .append(createDOMElement('section', '', '', 'WALK PIC'))
                .append(createDOMElement('p', '', '', "Walk Time: " + startTime))
                .append(createDOMElement('p', '', '', "Walk Dist: " + distance))
    
            // now iterate over every bus line
            lines.each(function() {
               var el = $(this);
               var busCode = el.attr('code').slice(1,4);
               container.append(createDOMElement('p', '', '', 'BUS PIC'))
                   .append(createDOMElement('p', '', '', 'Bus Number: ' + busCode));
               var stops = el.find('STOP');
               var stops_list = []; // this will hold bus stop names
    
               // now iterate over every bus stop 
                stops.each(function() {
                   var el = $(this).find('NAME'); 
                   stops_list.push(el.attr('val'));
                });
                container.append(createDOMElement('p', '', '', 'Bus Stops: ' + stops_list.join(',')));
            });
        });​
    }
    

    Note: if .find() matches multiple elements it returns a set of matched elements. For example el.find('WALK') in the example above actually contains two elements:

    el.find('WALK')[0].find('LENGTH').attr('time') == 3.540
    el.find('WALK')[1].find('LENGTH').attr('time') == 1.743
    

    If you don’t specify an index, you will be dealing with the first element

    el.find('WALK').find('LENGTH').attr('time') == 3.540
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am parsing XML using jQuery. I want to get the count of all
I have multiple xml files that I am trying to parse using JQuery. So
In my application, I am parsing XML data using SAX Parser. But, I want
How to parse xml element using attribute value. i am using Dom for parsing
I'm parsing a XML using jQuery with the following code: function appendNav(xml) { $(xml).find(Nav).each(function()
I'm having trouble parsing this xml file using jquery: <?xml version=1.0 encoding=utf-8?> <?mso-infoPathSolution name=urn:schemas-microsoft-com:office:infopath:BOT-Memos:-myXSD-2011-07-13T14-29-57
i am parsing Xml using ajax function in jquery in a jsp file $.ajax({
I have an XML web service that javascript calls are made using jQuery. This
I am parsing an xml file using jQuery and need to be able to
how to parse HTML tag using XML parsing I am parsing XML file but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.