I am having some trouble parsing xform xml with javascript.
The structure of the root of the xml is:
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:ex="http://www.w3.org/2001/xml-events" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa">
<h:head>
<h:title>Phonegap Survey</h:title>
</h:head>
<h:body>A ton more tags here</h:body>
</h:html>
I am using firefox (v.12) to develop and trying to deploy to phonegap/cordova on android. I am getting the data from an odk server using jquery ajax. I am specifiying dataType: 'xml' and it is responding with a document object as expected. In firefox I have written an app that presents data from this xml to the user. I am using built in javascript DOM functions like:
var title = surveyXML.getElementsByTagName('h:title')[0].firstChild.data
var body = surveyXML.getElementsByTagName('h:body')[0];
var text = surveyXML.getElementsByTagName('text')
I have written the full app (and it works fine) using these methods. But now that I have copied my files over to phonegap (running cordovo 1.7.0, on android emulator 2.3) on eclipse none of the DOM calls are returning elements!! I get something like:
05-31 13:21:28.686: D/CordovaLog(841): file:///android_asset/www/formcontroller.js:
line 159 : TypeError: Result of expression 'body' [undefined] is not an object.
For all the calls. I have verified that the device has the correct xml (it’s not empty or anything)
So having to get this done I tried to use jQuery to navigate the document object hoping it knew something I didn’t. Using calls like:
$(surveyXML).find('title');
$(surveyXML).find('h:title')
do not work at all. But tags without the h: prefix work fine, if I were to search for $(surveyXML).find('text') it returns all text elements as expected.
Seeing as the root elements are <html> I tried to specify dataType: html (just to try it even though the document is clearly marked as <?xml version="1.0"?>) and jquery is unable to parse it, as expected.
So I am wondering: How can I parse this XML cross platform so that it can work both in a browser and phonegap. And, supposing that it can’t work in both using the same DOM manipulation functions, how can I at least make it work in phonegap??
As always any help is appreciated. Thank you.
EDIT: For right Now I am just manually referencing the exact locations of the elements like to get to body I am going xml.firstChild.children[1]. Because I am underdeadline. I def feel like there should be a way to use getElementsByTagName still. ty.
This has been unanswered for 2 weeks so I am just posting what I did.
For right Now I am just manually referencing the exact locations of the elements like to get to body I am going xml.firstChild.children[1].
So all I did was not search for tags with the namespace and just reference them directly.