Answered by:
String / Text to XML

Question
-
Hi guys been at this many hours now and don't know what it is I am doing wrong, seen an example here on the forums that says it work but I just can seem to figure it out, read the documentation on loadXML() and xml document but no dice yet. The error I am getting is
"0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'getElementsByTagName'"
I think the reason I am getting this is because the string I am tring to convert to xml and store in the xmldocument var is simply not there.
WinJS.xhr({ url: "http://www.somewebsiteAPI.com/?t=" + movieName[i] + "&r=xml", responseType: "xml" }) .done( function (request) { var stringXML = request.responseText; var xmlDocument = new Windows.Data.Xml.Dom.XmlDocument(); xmlDocument.loadXml(stringXML); var posterURL = stringXML.getElementsByTagName("poster"); getPoster(posterURL); });
Also i can see the contents of stringXML and can comfirm they are correct have also check the contents of xmlDocument and cannot see any thing in there.
Cheers, - Anton.
Friday, July 27, 2012 4:28 PM
Answers
-
- Marked as answer by Jeff SandersMicrosoft employee, Moderator Monday, July 30, 2012 7:11 PM
Monday, July 30, 2012 11:39 AMModerator -
You can do something like this:
var xmlDocument = new Windows.Data.Xml.Dom.XmlDocument(); xmlDocument.loadXml("<root response=\"True\"><movie title=\"Terminator 2: Judgment Day\" imdbID=\"tt0103064\" imdbVotes=\"360,381\" imdbRating=\"8.6\" poster=\"http://ia.media-imdb.com/images/M/MV5BMTg5NzUwMDU5NF5BMl5BanBnXkFtZTcwMjk2MDA4Mg@@._V1_SX640.jpg\" plot=\"The cyborg who once tried to kill Sarah Connor must now protect her teenage son, John Connor, from an even more powerful and advanced Terminator.\" actors=\"Arnold Schwarzenegger, Linda Hamilton, Edward Furlong, Robert Patrick\" writer=\"James Cameron, William Wisher Jr.\" director=\"James Cameron\" genre=\"Action, Sci-Fi, Thriller\" runtime=\"2 h 32 min\" released=\"03 Jul 1991\" rated=\"R\" year=\"1991\"/></root>"); var posters = xmlDocument.getElementsByTagName("movie"); for (var i = 0; i< posters.length; i++) { var posterValue = posters[i].attributes.getNamedItem("poster").nodeValue; }
-JeffJeff Sanders (MSFT)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Monday, July 30, 2012 7:11 PM
- Marked as answer by Anton2k Monday, July 30, 2012 7:28 PM
Monday, July 30, 2012 7:11 PMModerator
All replies
-
Hi Anton,
You will be embarrassed sorry... stringXML does not have the method getElementsByTagName (as the error is telling you). Only XmlDocument objects have that method.
stringXML is not an XmlDocument. xmlDocument is. Change your code to use the xmlDocument object you created.
-Jeff
Jeff Sanders (MSFT)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Friday, July 27, 2012 7:53 PM
Friday, July 27, 2012 7:53 PMModerator -
Hi jpsanders didn't notice that, i still cant seem to get the stringXML to load into my xmlDocument object though. Set a break point and check the xmlDocument and its empty at the point were it should be populated with xml.
Cheers, - Anton.
Friday, July 27, 2012 8:59 PM -
Here is what I am having a problem with.
var stringXML = request.responseText; var xmlDocument = new Windows.Data.Xml.Dom.XmlDocument(); xmlDocument.loadXml(stringXML);
to me this should work. I have made sure I am getting the expected string in stringXML, the xmlDocument after the loadXml has no xml in it I have checked.- Edited by Anton2k Sunday, July 29, 2012 1:09 PM
Sunday, July 29, 2012 1:08 PM -
- Marked as answer by Jeff SandersMicrosoft employee, Moderator Monday, July 30, 2012 7:11 PM
Monday, July 30, 2012 11:39 AMModerator -
Hey JP thanks for the reply, yea that works fine for me to. Have no idea what could be up with the xml, once i find out though i probably could modify the variable the string is being stored in before passing it to load xml. I am not too much of an xml guru but from what i can see the api that i am communicating with is returning valid xml. Here is a sample of what i am getting, though i may be better asking this in a xml specific forum, as a side not i noticed a thread on here talking about xml headers causing a problem somthing to do with utf formatting or somthing like that, but i dont know enough about xml to know exactly what or how i could fix it, i also couldn't find any documentation saying what type of formate the loadxml function takes.
<?xml version="1.0" encoding="UTF-8"?> -<root response="True"><movie title="Terminator 2: Judgment Day" imdbID="tt0103064" imdbVotes="360,381" imdbRating="8.6" poster="http://ia.media-imdb.com/images/M/MV5BMTg5NzUwMDU5NF5BMl5BanBnXkFtZTcwMjk2MDA4Mg@@._V1_SX640.jpg" plot="The cyborg who once tried to kill Sarah Connor must now protect her teenage son, John Connor, from an even more powerful and advanced Terminator." actors="Arnold Schwarzenegger, Linda Hamilton, Edward Furlong, Robert Patrick" writer="James Cameron, William Wisher Jr." director="James Cameron" genre="Action, Sci-Fi, Thriller" runtime="2 h 32 min" released="03 Jul 1991" rated="R" year="1991"/></root>
Thank's - Antony.
Monday, July 30, 2012 2:52 PM -
I think that's me got it jp, it looks like it was the xml header that was causing a problem so I just sub stringed it out for the time being until I find a more rock solid soulution, but now I have to figure out how to get the value of a elements attribute, upon further inspection of the xmldocument I setup that I have loaded the xml from a string into I can see that attributes are not reffered to in there names but rather they are numbered a bit like a array 0 being the starting attribute of an element. Trying to find how I can get the value of them.Monday, July 30, 2012 6:44 PM
-
You can do something like this:
var xmlDocument = new Windows.Data.Xml.Dom.XmlDocument(); xmlDocument.loadXml("<root response=\"True\"><movie title=\"Terminator 2: Judgment Day\" imdbID=\"tt0103064\" imdbVotes=\"360,381\" imdbRating=\"8.6\" poster=\"http://ia.media-imdb.com/images/M/MV5BMTg5NzUwMDU5NF5BMl5BanBnXkFtZTcwMjk2MDA4Mg@@._V1_SX640.jpg\" plot=\"The cyborg who once tried to kill Sarah Connor must now protect her teenage son, John Connor, from an even more powerful and advanced Terminator.\" actors=\"Arnold Schwarzenegger, Linda Hamilton, Edward Furlong, Robert Patrick\" writer=\"James Cameron, William Wisher Jr.\" director=\"James Cameron\" genre=\"Action, Sci-Fi, Thriller\" runtime=\"2 h 32 min\" released=\"03 Jul 1991\" rated=\"R\" year=\"1991\"/></root>"); var posters = xmlDocument.getElementsByTagName("movie"); for (var i = 0; i< posters.length; i++) { var posterValue = posters[i].attributes.getNamedItem("poster").nodeValue; }
-JeffJeff Sanders (MSFT)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Monday, July 30, 2012 7:11 PM
- Marked as answer by Anton2k Monday, July 30, 2012 7:28 PM
Monday, July 30, 2012 7:11 PMModerator -
That worked a treat! thanks very much jp you have been an amazing help!
- Anton.
Monday, July 30, 2012 7:29 PM