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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:46:25+00:00 2026-05-31T17:46:25+00:00

I am writing a XML file into a HTML file using JavaScript. Here is

  • 0

I am writing a XML file into a HTML file using JavaScript.

Here is the JavaScript I am using:

if(window.XMLHttpRequest){
   xmlhttp=new XMLHttpRequest();
}
else{
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","file.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

var x=xmlDoc.getElementsByTagName("tagParent");

document.write(x.getElementsByTagName("tagChild")[0].childNodes[0].nodeValue);

The script will working for everything except for self closing xml elements (<element/>).

Example XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<tagParent>
    <tagChild/>
</tagParent>

The script breaks and stops when it reaches a self closing tag.

What do I need to do to get it either output 0 or “”? Why is it breaking?

  • 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-05-31T17:46:27+00:00Added an answer on May 31, 2026 at 5:46 pm

    It is breaking because the element is empty. It doesn’t have any children so when you try to get the nodeValue of the first child, it errors because undefined doesn’t have a 0 property.

    You want something along the lines of:

    var dataNodes, value;
    dataNodes = x.getElementsByTagName("tagChild")[0].childNodes;
    if (dataNodes.length) {
        value = dataNodes[0].nodeValue;
    } else {
        value = 0;
    }
    document.write(value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem writing Norwegian characters into an XML file using C#. I
I am writing an XSLT file to translate an XML document into an XHTML
I'm writing an XML file with the following code: Source source = new DOMSource(rootElement);
What format do I use for Date/Time when writing to an XML file using
I'm populating an XElement with information and writing it to an xml file using
I am writing my first windows ce app. I am using an xml file
I am writing lots of info from an XML file into a database. Everything
I'm writing a windows service that import an XML file into a SQLite database.
While implementing XML file reading/writing in my application I saw that when I call
I'm writing an XSLT template that need to output a valid xml file for

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.