PDA

View Full Version : Trying to parse simple XML with YUI, thinking WTF.



jlizarraga
09-01-2008, 10:47 AM
Hi all.

I want to use Yahoo's YUI DataSource utility to parse my XML data, but I'm having trouble understanding the parameters of the function that parses the data!

YUI is really good about giving solid examples for their stuff, but an example for simple XML seems to have been overlooked. Here is the page where they provide examples:

http://developer.yahoo.com/yui/datasource/

And here is the ONLY documentation I could find anywhere of DataSource's parseXMLData method:

http://developer.yahoo.com/yui/docs/YAHOO.util.DataSource.html#method_parseXMLData

And finally, here is the code I have come up with based on those examples:


var invData = new YAHOO.util.DataSource("http://www.example.com/example.xml");
invData.responseType = YAHOO.util.DataSource.TYPE_XML;

invData.responseSchema = {
resultNode: "result",
fields: [
"vin",
"stock",
{key: "year", parser: YAHOO.util.DataSource.parseNumber}
]
};

var invListing = invData.parseXMLData(oRequest,oFullResponse);

I'm stumped on what to do with oRequest and oFullResponse.

Any help greatly appreciated! Oh, and happy Labor Day everyone!

jlizarraga
09-01-2008, 10:51 AM
Oh, and the XML:


<results>
<result
vin = "PCF168FC9LWIZPY"
stock = "2DTSZ"
year = "2008"
/>
</results>

jlizarraga
09-01-2008, 06:41 PM
So I think my problem stems from the fact that this DataSource utility was intended for making calls to a URL that produces different content depending on its parameters, whereas I'm using just using a completely static XML file.

I'm now looking into these libraries:

http://xmljs.sourceforge.net/

http://dev.abiss.gr/sarissa/

Any advice on which to use?

jlizarraga
09-01-2008, 07:35 PM
Well, I decided against xmljs because it requires you to format the type of XML I'm loading in a weird way, so I'm going to try Sarissa. Results later!

jlizarraga
09-01-2008, 09:04 PM
Okay, I've finally got access to my XML with this, which works great in FF and IE, but not in Safari:


function loadXMLDoc(dname)
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
var oMSIE = true;
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument("","",null);
var oMSIE = false;
}
catch(e) {alert(e.message)}
}
try
{
xmlDoc.async=false;
xmlDoc.load(dname);
if (oMSIE == false){
alert(xmlDoc.childNodes[0].nodeName);
} else if (oMSIE == true){
alert(xmlDoc.childNodes[1].nodeName);
}
return(xmlDoc);
}
catch(e) {alert(e.message)}
return(null);
}

var oXML = loadXMLDoc("http://autofusion.com/development/mm3/example_results/example_feed.xml"); // alerts "results" in FF and IE, "Value undefined (result of expression xmlDoc.load) is not object" in Safari.

If anyone knows how to get this function working in Safari, it would be greatly appreciated!

jlizarraga
09-01-2008, 10:38 PM
Here is the W3C example, which produces the same error in Safari (3.1.2):

http://www.w3schools.com/DOM/tryit.asp?filename=try_dom_loadxmldoc