Use Dom to process XML documents (zz)

Source: Internet
Author: User
Tags xsl

XML file 1
<? Xml version = "1.0" standalone = "yes"?>
<Tree>
<TreeNode>
<NodeId> 0 </NodeId>
<Title> replacement </Title>
<NodeXmlSrc> Content. xml </NodeXmlSrc>
</TreeNode>
<TreeNode>
<NodeId> 0 </NodeId>
<Title> brand </Title>
<NodeXmlSrc> BrandList. xml </NodeXmlSrc>
</TreeNode>
<TreeNode>
<NodeId> 0 </NodeId>
<Title> type </Title>
<NodeXmlSrc> SpecContent. xml </NodeXmlSrc>
</TreeNode>
<TreeNode>
<NodeId> 0 </NodeId>
<Title> customer service </Title>
<NodeXmlSrc> BuyerList. xml </NodeXmlSrc>
</TreeNode>
<TreeNode>
<NodeId> 0 </NodeId>
<Title> rules </Title>
<NodeXmlSrc> SpecList. xml </NodeXmlSrc>
</TreeNode>
<TreeNode>
<NodeId> 0 </NodeId>
<Title> China </Title>
<NodeXmlSrc> aa. xml </NodeXmlSrc>
</TreeNode>
</Tree>

XML file 2
<? Xml version = "1.0" standalone = "yes"?>
<Tree>
<Treenode nodeid = "0" Title = "" nodexmlsrc = "buyerlist. xml"> </treenode>
<Treenode nodeid = "0" Title = "brand" nodexmlsrc = "sample. xml"> </treenode>
<Treenode nodeid = "0" Title = "" nodexmlsrc = "msdnlib587 _. xml"> </treenode>
<Treenode nodeid = "0" Title = "" nodexmlsrc = "msdnlib3 _. xml"> </treenode>
<Treenode nodeid = "0" Title = "" nodexmlsrc = "msdnlib3 _. xml"> </treenode>
<Treenode nodeid = "0" Title = "" nodexmlsrc = "msdnlib587 _. xml"> </treenode>
<Treenode nodeid = "0" Title = "China" nodexmlsrc = "123456.xml"> </treenode>
</Tree>

What I'm talking about is their structure. Why is it so different, but it is the same as when it comes to a table,
File 2 is changed to file 1

Are the structures of the two files significantly different? Are there any differences?
The main problem is how to convert.
You can use Dom to parse the second file and then construct the first file.

Try:
/*** A.htm ***/
<Script language = "javaScript">
Function transXML ()
{
Var oXML, oXSL;
Var sXML;

OXML = new ActiveXObject ("MSXML2.DOMDocument. 3.0 ");
OXSL = new ActiveXObject ("MSXML2.DOMDocument. 3.0 ");
OXML. async = false;
OXML. load ("a. xml ");
OXSL. async = false;
OXSL. load ("a. xsl ");

Alert (oXML. xml); // before conversion

SXML = oXML. transformNode (oXSL)

Alert (sXML); // After conversion
}

Function window. onload ()
{
TransXML ();
}
</SCRIPT>

/*** A. xml ***/
<? Xml version = "1.0" encoding = "UTF-8"?>
<Tree>
<TreeNode NodeId = "0" Title = "" NodeXmlSrc = "BuyerList. xml"> </TreeNode>
<TreeNode NodeId = "0" Title = "brand" NodeXmlSrc = "Sample. xml"> </TreeNode>
<TreeNode NodeId = "0" Title = "" NodeXmlSrc = "msdnlib587 _. xml"> </TreeNode>
<TreeNode NodeId = "0" Title = "" NodeXmlSrc = "msdnlib3 _. xml"> </TreeNode>
<TreeNode NodeId = "0" Title = "" NodeXmlSrc = "msdnlib3 _. xml"> </TreeNode>
<TreeNode NodeId = "0" Title = "" NodeXmlSrc = "msdnlib587 _. xml"> </TreeNode>
<TreeNode NodeId = "0" Title = "China" NodeXmlSrc = "123456.xml"> </TreeNode>
</Tree>

/*** A. xsl ***/
<? Xml version = "1.0" encoding = "UTF-8"?>
<Xsl: stylesheet version = "1.0" xmlns: xsl = "http://www.w3.org/1999/XSL/Transform">
<Xsl: output method = "xml" encoding = "UTF-8" indent = "yes"/>

<Xsl: template match = "/">
<Tree>
<Xsl: apply-templates select = "Tree/TreeNode"/>
</Tree>
</Xsl: template>

<Xsl: template match = "TreeNode">
<Xsl: element name = "{name ()}">
<Xsl: for-each select = "@ *">
<Xsl: element name = "{name ()}">
<Xsl: value-of select = "."/>
</Xsl: element>
</Xsl: for-each>
</Xsl: element>
</Xsl: template>

</Xsl: stylesheet>

Under/Tree/TreeNode,
"XML file 1" describes the features of the current TreeNode using element.
"XML file 2" describes the features of the current TreeNode with attribute
This leads to structural differences

Element and attribute can be defined as XmlNode in DOM, but different from NodeType, their content processing methods are also different.

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.