/    Sign up×
Community /Pin to ProfileBookmark

"firstChild.nodeValue"–nothing?!!

//here is my code.
//XMLFile.xml
<?xml version=”1.0″ encoding=”utf-8″ ?>
<peoples>
<person>
<name>A</name>
<sex>M</sex>
<mobile>11111111111</mobile>
</person>
<person>
<name>B</name>
<sex>F</sex>
<mobile>22222222222</mobile>
</person>
</peoples>

//js.js

if(window.ActiveXObject){
var doc=new ActiveXObject(“Microsoft.XMLDOM”);
doc.async=”false”;
doc.load(“XMLFile.xml”);
}
else if(document.implementation&&document.implementation.createDocument){
var parser=new DOMParser();
var doc=parser.parseFromString(“XMLFile.xml”, “text/xml”);
}
else{
throw new Error(“XML DOM is not supported!”);
}

var rs=””;
var name;
var x=doc.getElementsByTagName(‘person’);
for(var i=0; i< x.length; i++){
name=x[i].firstChild.firstChild.nodeValue;
rs+=name + “, “;
}
document.write(rs);

//why doesn’t it work? Nothing is displayed in the browser. Why??
//Help!!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@captainscallJul 25.2012 — ActiveX is out of date (and only worked for IE).... Use...
[CODE]
some_ajax_request = new XMLHttpRequest(); //Modern browsers support it.
some_ajax_request.open("GET", yourxmlfile.xml);

etc..
[/CODE]


http://www.w3schools.com/xml/xml_http.asp
Copy linkTweet thisAlerts:
@rtretheweyJul 25.2012 — This line:
<i>
</i>name=x[i].firstChild.firstChild.nodeValue;

tries to fetch the first child of the first child in every 'person' node - in other words, a node contained within the first child node - and there aren't any. Each 'person' node in your data contains three child simple nodes. If you want the 'name' node, loop through those child nodes until you find it.
Copy linkTweet thisAlerts:
@007JulienJul 26.2012 — There is different node types and some [I]&#171; illegitime nodes &#187;[/I] like carriage returns, tabs or other specials characters which are to avoid to work with element Nodes (see, for example, this page about the different node Types).

Then the two different following syntax give with Mozilla FireFox different firstChild Nodes !

[CODE]
[COLOR="RoyalBlue"]// First syntax with line feed and carriage return[/COLOR]
<person>
<name>A</name>
<!-- -->
</person>

[COLOR="RoyalBlue"]// Second syntax without characters between the two element Nodes[/COLOR]
<person><name>A</name>
<!-- -->
</person>
[/CODE]
Then to get the first element Node of your person tag (even if the code contains comments), you have to make something like this[CODE]
var j=0,x=doc.getElementsByTagName('person');
while (x.childNodes[j].nodeType!=1) j++;
var xFirstElementNode=x.childNodes[j];
[/CODE]
Copy linkTweet thisAlerts:
@007JulienJul 26.2012 — Sorry, I forgot the collection and the loop ! Replace x by x[i] in the last lines...
×

Success!

Help @kebikill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...