/    Sign up×
Community /Pin to ProfileBookmark

JavaScript & XML

I have a XML like this

[code]
<?xml version=”1.0″ encoding=”UTF-8″?>
<family>
<parent pos=”1″>
<name></name>
<birth></birth>
<death></death>
<fwife>
<name></name>
<birth></birth>
<death></death>
</fwife>
<sons>
<parent pos=”11″>
<name></name>
<birth></birth>
<death></death>
<fwife>
<name></name>
<birth></birth>
<death></death>
</fwife>
<sons>

</sons>
</parent>
</sons>
</parent>
</family>
[/code]

Someone could tell me a way to get a parent node based on the pos attribute with JavaScript, please?

I know that I can use the getAttribute and loop, but is there a more efficient way? This xml can become a very large file.
Cheers ?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Orc_ScorcherJan 24.2007 — You can use XPath for tasks like this. An example to find the parent with pos="11" (assumes 'xml' is a reference to your XML document): var xpathExpr = "//parent[@pos = '11']"
try {
// Try DOM XPath first.
var node = xml.evaluate(xpathExpr, xml, null, XPathResult.FIRST_ORDERED_NODE_TYPE,
null).singleNodeValue
}
catch (e) {
try {
// Try IE's selectSingleNode method.
node = xml.selectSingleNode(xpathExpr)
}
catch (e) {
// Didn't work either, implement some fallback action here.
}
}
Copy linkTweet thisAlerts:
@cyberowlauthorJan 25.2007 — Works like a charm Orc Scorcher. Thanks a lot. Don't want to annoy you but could you point me some site where this function is detailed?

I like to know exactly how it works. Have been looking around for it but didn't find a single reference in google.
Copy linkTweet thisAlerts:
@cyberowlauthorJan 25.2007 — Hum, is there a similar string to get multiple nodes? I would like to retrieve all the wives that someone had. I put a property husband in the fwife tag.
Copy linkTweet thisAlerts:
@Orc_ScorcherJan 25.2007 — [url=http://msdn2.microsoft.com/en-us/library/ms754523.aspx]Description of selectNodes[/url], has a link to selectSingleNode.

[url=http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator]Description of evaluate[/url] and the rest of the XPath DOM.

For XPath itself there are tons of tutorials on the net, pick one you like but make sure it describes XPath 1.0, not 2.0.
×

Success!

Help @cyberowl 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.17,
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,
)...