0

I'm parsing an XML file and having issues that seem to have to do with carriage returns (or maybe line feeds) and tabs (or white space) in the XML file. They trip up my JavaScript and return nothing instead of the data.

Here's a snippet of the JS code:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (xhttp.readyState == 4 && xhttp.status == 200) {
    fullXMLContent = xhttp.responseXML;
  }
};

xhttp.open("GET", xmlPathname, true);
xhttp.send();

var navLocationMain = fullXMLContent.getElementsByTagName("locationMainTitle");
projectLocationMain = navLocationMain[0].childNodes[0].data;
document.getElementById("Nav_Top_Left-courseLocationMain").innerHTML = projectLocationMain;

This XML works and returns "INTRODUCTION":

<locationMainTitle><![CDATA[<div style="margin-top: 12px">INTRODUCTION </div>]]></locationMainTitle>

This XML doesn't work and returns nothing:

<locationMainTitle>
 <![CDATA[<div style="margin-top: 12px">INTRODUCTION </div>]]>
</locationMainTitle>

The only difference is the carriage returns and tabs in the second example. This happens in all browsers I've tested so far.

I'm trying to preserve the readability and easier editing of the formatting that includes carriage returns and tabs in the XML file.

I saw one reference that mentioned a CR, LF, tab or white space can cause problems in parsing XML but I don't know how to get around that with my JS an still preserve the formatting in the XML file.

daddioJP
  • 1
  • 1
  • Look at this http://stackoverflow.com/questions/2265966/xml-carriage-return-encoding – Badr Mar 18 '16 at 14:13
  • Why is the
    being returned as CDATA and not the entire string? Also, does your HTML file have the XHTML DOCTYPE?
    – Scott Marcus Mar 18 '16 at 14:16
  • Thanks for the link, @BadruzZaman - I looked there before and that seems to be about inserting CR/LF. My issue is trying to determine why CR/LF and whitespace don't return anything while leaving them out does. – daddioJP Mar 18 '16 at 17:09
  • @ScottMarcus, using CDATA does return the entire string, including the HTML div markup, but only when no CR/LF and white space is between the XML tags. I'm sending the output to an HTML element. – daddioJP Mar 18 '16 at 17:14

0 Answers0