Dom parsing differs in Java and JavaScript

Source: Internet
Author: User

Summarize the differences between using DOM to parse XML files in Java and JavaScript

1. Different ways to generate DOM document objects

A in JavaScript, create the DOM document object code as follows:

Creating an XML document

Varxmldoc = new ActiveXObject ("Microsoft.XMLDOM");

Setting does not support asynchronous

Xmldoc.async= false;

Load XML file

Xmldoc.load ("Demo03.xml");

B in Java, create the DOM document object code as follows:

Create a parser Project object

Publicdocument document;

Documentbuilderfactory factory =documentbuilderfactory.newinstance ();

Create a Parser object

Documentbuilderbuilder = Factory.newdocumentbuilder ();

Create an XML file in memory of the tree node model

Document=builder.parse (DomReaderXML.class.getClassLoader (). getResourceAsStream ("XML file"));

2. Gets the root node and the property name and the property value method

C. Get in javascript:

Varroot = xmldoc.documentelement//Get root node

varnode=xmldoc.documentelement.childnodes[0];//nodes under the root node

document.write ("<br/>===" +node.basename);//basename, for output node labels

node type, where 1 represents the element node, 2 represents the attribute node, and 3 represents the text node

document.write ("<br/> node type:" +node.nodetype+ "<br/>");

document.write ("root element node name:" +node.ownerdocument.documentelement.tagname+ "<br/>");

document.write ("Property name of the current node:" +node.attributes[0].name+ "<br/>");

document.write ("Property value of current node:" +node.attributes[0].value);

document.write ("<br/> the text contained in the current node and its child nodes" +node.text);

D in Java to obtain:

Convert data in an XML document into a collection object

Get all Student node collections

Nodelistnodes = document.getElementsByTagName ("student");

List<student>stulist = new arraylist<student> ();

Label name and attribute for loop output node

for (int i=0;i<nodes.getlength (); i++) {

Student stu = new Student ();

Convert to Element elements object

element element = (Element) nodes.item (i);

System.out.println (Element.getnodename ());

Property value

String id = element.getattribute ("id");

Stu.setid (ID);

NodeList Childs =element.getchildnodes ();

String name =document.getelementsbytagname ("name"). Item (i). Getfirstchild (). Getnodevalue ();

Stu.setname (name);

String strage =document.getelementsbytagname ("Age"). Item (i). Getfirstchild (). Getnodevalue ();

Stu.setage (Integer.parseint (strage));

String sex = document.getelementsbytagname ("Sex"). Item (i)-getfirstchild (). Getnodevalue ();

Stu.setsex (Sex);

Stulist.add (Stu);

}

After the load is complete, the XML file is used to find the node.

NodeList nodelist = document.getelementsbytagname ("student");

for (int i =0; I<nodelist.getlength (); i++) {

Elementstuelement = (Element) nodelist.item (i);

Get the value of a property

String idvalue =stuelement.getattribute ("id");

SYSTEM.OUT.PRINTLN ("id attribute value" +idvalue);

NodeList child =stuelement.getchildnodes ();

for (int j=0;j<child.getlength (); j + +) {

if (Child.item (j). Getnodetype () ==node.element_node) {

System.out.println (Node.element_node);

System.out.println (Child.item (j). Getnodetype ());

Node node = Child.item (j); System.out.println (Node.getnodename () + "= =" +node.gettextcontent ());

}

}

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.