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>