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 8066027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:51:08+00:00 2026-06-05T11:51:08+00:00

I have multiple xml files that I am trying to parse using JQuery. So

  • 0

I have multiple xml files that I am trying to parse using JQuery. So far, I’ve only found success by parsing each file individually. I’m new at this and my code is very long and repetitive. I’d appreciate any help scaling it down. Here is my js:

$.ajax({
    type: "GET",
    url: "docs/doc1.xml",
    dataType: "xml",
    success: parseXml1
});

$.ajax({
    type: "GET",
    url: "docs/doc2.xml",
    dataType: "xml",
    success: parseXml2
});
$.ajax({
    type: "GET",
    url: "docs/doc3.xml",
    dataType: "xml",
    success: parseXml3
});
function parseXml1(xml){
    var gen = $(xml).find("subsystem").eq(0);
    var title = gen.find("title").text();
    var text = gen.find("text").text();
    $("#Title01").html(title);
    $("#Text01").html(text);
};

function parseXml2(xml){
    var gen = $(xml).find("subsystem").eq(0);
    var title = gen.find("title").text();
    var text = gen.find("text").text();
    $("#Title02").html(title);
    $("#Text02").html(text);

    var sys = $(xml).find("subsystem").eq(1);
    var title = sys.find("title").text();
    var text = sys.find("text").text();
    $("#Title02-1").html(title);
    $("#Text02-1").html(text);
};

function parseXml3(xml){
    var gen = $(xml).find("subsystem").eq(0);
    var title = gen.find("title").text();
    var text = gen.find("text").text();
    $("#Title03").html(title);
    $("#Text03").html(text);

    var sys = $(xml).find("subsystem").eq(1);
    var title = sys.find("title").text();
    var text = sys.find("text").text();
    $("#Title03-1").html(title);
    $("#Text03-1").html(text);
};

And my xml is set up as follows:

<root>
   <subsystem>This is information 1</subsystem>
   <subsystem>This is information 2</subsystem>
</root>

So I have multiple nodes with no attributes that I am trying to access one by one within each xml file. Then, I am trying to put that text into a div on my HTML page. There has to be a better way to do this.

  • 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-05T11:51:09+00:00Added an answer on June 5, 2026 at 11:51 am

    Make it a function call

    function parseFile( path, titleId, textId) {
    
        function parseXml(xml){
            var subSystems = $(xml).find("subsystem");
            var gen = subSystems.eq(0);
            var title = gen.find("title").text();
            var text = gen.find("text").text();
            $("#" + titleId).html(title);
            $("#" + textId).html(text);
            if ( subSystems.length===2 ) {
                var sys = subSystems.eq(1);
                var title = sys.find("title").text();
                var text = sys.find("text").text();
                $("#" + titleId + "-1").html(title);
                $("#" + textId + "-1").html(text);
            }
        };
    
        $.ajax({
            type: "GET",
            url: path,
            dataType: "xml",
            success: parseXml
        });
    }
    
    parseFile("docs/doc1.xml", "Title01", "Text01");
    parseFile("docs/doc2.xml", "Title02", "Text02");
    parseFile("docs/doc3.xml", "Title03", "Text03");
    

    and you can make it smaller, by getting rid of that if check and doing a loop

    function parseFile( path, titleId, textId) {
    
        function parseXml(xml){
    
            $(xml).find("subsystem").each( 
                function(ind) {
                    var gen = jQuery(this);
                    var title = gen.find("title").text();
                    var text = gen.find("text").text();
                    var ext = ind===0 ? "" : "-" + ind;
                    $("#" + titleId + ext).html(title);
                    $("#" + textId + ext).html(text);
                }
            );
        };
    
        $.ajax({
            type: "GET",
            url: path,
            dataType: "xml",
            success: parseXml
        });
    }
    
    parseFile("docs/doc1.xml", "Title01", "Text01");
    parseFile("docs/doc2.xml", "Title02", "Text02");
    parseFile("docs/doc3.xml", "Title03", "Text03");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a folder structure that contains multiple javascript files, each of these files
I'm trying to combine multiple, smaller XML files into one large XML. I have
I have a controller that I am trying to grab XML files from remote
I have an XML valued column that has multiple parent nodes. I need to
I have multiple (8) WAR files and 1 EAR file that I want to
I'm trying to implement an algorithm to search multiple XML files for a certain
I have a large application spread across multiple Spring bean definition xml files. In
I have a T4 that is generating multiple .html files. After creating them all
I have an XML file that I'm trying to validate against an XSD file
Background: In our project, we have a bunch of xml files that define tests

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.