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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:46:25+00:00 2026-06-16T05:46:25+00:00

I’l just trying to pick up using the HttpXMLRequest object in JS and I’ve

  • 0

I’l just trying to pick up using the HttpXMLRequest object in JS and I’ve got a basic loop that loads name and age from an XML file and throws it into a table. If each sibling has the same values then it loads fine, however if one of the elements is missing from a sibling it stops rather than just skips it. Heres some code.

The HTML –

(The rest of the XML parser and DTD, html, body etc is up here)
<script>
var x=xmlDoc.getElementsByTagName("person");

document.write("<table border='1'>");
for (i=0;i<x.length;i++)
  { 
  var name = x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
  var age = x[i].getElementsByTagName("age")[0].childNodes[0].nodeValue;

  document.write("<tr>");
  document.write("<td>"+name+"</td>");
  document.write("<td>"+age+"</td>");
  document.write("</tr>");
  }
document.write("</table>");
</script>

The XML –

(The DTD and stuff it up here)
<people>
  <person>
    <name>Jim</name>
    <age>20</age>
  </person>
  <person>
    <name>Bob</name>
  </person>
  <person>
    <name>Tom</name>
    <age>20</age>
  </person>
  <person>
    <name>James</name>
    <age>20</age>
  </person>
  <person>
    <name>Richie</name>
    <age>20</age>
  </person>
</people>

From that example, the HTML will stop after writing once it gets to bob, I assume there needs to be a try/catch or an if in there for each name and age, but I can’t seem to place it so it works. Anybody got any ideas?

I’ve tried to search but I don’t know what this issue is called, and describing it doesn’t come up with anything.

Simply adding “<age /> or <age></age>” isn’t an option, eventually I’ll be working with massive xml files. Unless I need to write something that will automatically add it if it doesn’t exist, that’s acceptable I guess.

Cheers Hombries.

  • 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-16T05:46:27+00:00Added an answer on June 16, 2026 at 5:46 am

    Check existence of element before trying to access its child nodes:

    var personElem = x[i];
    
    var ageElems = personElem.getElementsByTagName("age");
    
    var age = ageElems.length
            ? ageElems[0].firstChild.nodeValue
            : '';
    

    By the way, it’s typically easier to use JSON instead of XML. With JSON, your response could look like this:

    {
        "people": [
            {
                "name" : "Jim",
                "age" : 20
            },
            {
                "name" : "Bob"
            }
        ]
    }
    

    And you could access items with dot syntax:

    var age = data.people[0].age;
    

    For example, PHP has built-in function for encoding JSON: json_encode().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am using jsonparser to parse data and images obtained from json response. When
I have just tried to save a simple *.rtf file with some websites and

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.