Use javascript to read xml files and read node data. javascriptxml

Source: Internet
Author: User

Use javascript to read xml files and read node data. javascriptxml

The following code reads the node data, and read the node attribute data.


Javascript reads xml files

Don't use childNodes [x] to retrieve it. It will make you dizzy and have no idea which node to choose. Use selectSingleNode (xPath) to solve
For example, the third node you cannot read...
Var xn = xmlDoc2.documentElement. selectSingleNode ("ChannelState/Record ");
Alert (xn. text );
If you want to obtain the Signal node, it is
XmlDoc2.documentElement. selectSingleNode ("ChannelState/Signal ");
You can. This statement is much clearer. If you add other nodes to the channelState node, your code will not be affected.
This xPath is the path. The name of the node under the root node starts from the leftmost and is written layer by layer, which is similar to the path of the folder.

Javascript reads xml data

//////////////////////////////////////// ///////////////////
First, the content of the xml file (tree. xml) is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Treeview>
<Tree id = "p1">
<Text> Shandong </text>
<Target> _ blank </target>
<Title> province </title>
<Link> </link>
<Tree id = "p1-1">
<Text> Weihai </text>
<Target> _ blank </target>
<Title> city </title>
<Link> </link>
</Tree>
<Tree id = "p1-2">
<Text> Yantai City </text>
<Target> _ blank </target>
<Title> city </title>
<Link> </link>
<Node id = "p1-2-1">
<Text> Changyi village </text>
<Target> _ blank </target>
<Title> Villages and Towns </title>
<Link> www.baidu.com/</link>
</Node>
</Tree>
<Node id = "p1-3">
<Text> fuzhen </text>
<Target> _ blank </target>
<Title> Villages and Towns </title>
<Link>... the remaining full text>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.