views:

39

answers:

3

Hi, Actually I am just a starter of XML. In one of my task, I am doing the following:

var req = new XMLHttpRequest(); 
req.open('GET', 'http://www.mohin.com/test.xml', false);
req.send();  
var xmlObj = req.responseXML;

Now, I am getting the XML as:

<?xml version="1.0" encoding="utf-8"?>
 <Items status="as">
    <Song title="Helios (Original Mix)">
       <Artist name="Chris Hingher" ID="291728"></Artist>
       <Info StartTime="19:37:39" JazlerID="2219" PlayListerID="" />
    </Song>
    <Song title="I Was Drunk">
       <Artist name="Riva Starr Feat. N�ze" ID="292052"></Artist>
       <Info StartTime="19:45:38" JazlerID="2267" PlayListerID="" />
    </Song>
    .............
 </Items>

You can see that there is a special character in tag's name attribute in the second tag and for that I am getting a parsing error and for this I cant read the XML object. See this tag: <Artist name="Riva Starr Feat. N�ze" ID="292052"></Artist> and look at it's name attribute.

Is there anyone who can give me a solution to this issue? I really need to retrieve each artist name like this:

var info = xmlObj.getElementsByTagName("Song")[0];
var artistName = info.childNodes[j].attributes.getNamedItem('name').nodeValue;

Thanks Mohin

A: 

Have you checked the format of encoding of your XML file ? Could come from there.

Raveline
+1  A: 

When you create the XML file, save it using UTF-8 as well as just claiming it is saved using UTF-8.

David Dorward
Hi,Thanks a lot for your answer. Can't i resolve the issue from my code? i cant change or modify from where the xml is being generated and saved
Mohin
@Mohin the problem is, it's not well-formed XML. It's broken, buggy. There is always a point at which you can reject a piece of input as not workable. This may or may not be past that point. You might try software like tidy.sourceforge.net to clean up encoding problems.
LarsH
+3  A: 

It looks like the encoding used to save the XML document was not the one specified in the XML prolog (encoding="utf-8"). This can happen if the XML file is edited and saved manually by a dumb editor.

Assuming you can change the XML document, modify the encoding attibute from "utf-8' to "iso-8859-1". If it works, then you should be more careful about the encoding of you XML documents.

gawi
The problem is definitely at the server end. Just setting UTF-8 in the XML declaration isn't enough. You have to actually write the output using the UTF-8 encoding, otherwise it comes out in the platform default, which looks like, in this case, iso-8859-1, as @gawi correctly noted.
Jim Garrison
Hi,Thanks a lot for your answer. Can't i resolve the issue from my code? The XML is a software generated XML, so i cant change it or modify it
Mohin
You should contact the person in charge of the website generating the XML document. I can't think of a simple Javascript work-around for that kind of problem.
gawi