DOM parsing XML can be divided into three steps:
1. Create DocumentBuilderFactory.
DocumentBuilderFactory DBF = DocumentBuilderFactory.newinstance ();
DocumentBuilderFactory is used to create a DocumentBuilder object.
2. Create DocumentBuilder.
DocumentBuilder DB = dbf.newdocumentbuilder ();
3, parsing the file to create a Document object.
Document doc = db.parse (new file (file (fileXmlname)); DocumentBuilder's PARSE () method accepts an XML document name as an input parameter, returns a Document object, which represents a tree model of an XML document. All the operations of the XML document are not related to the parser, and it can be operated directly on this Document object. The method of specific operations of the Document is defined by the DOM.
The basic objects of the DOM have 5: Document, Node, Nodelist, Element, and Attr. The Document object represents the entire XML document. All other node are included in the Document object in a certain order, arranged as a tree structure, and the programmer can get all the contents of the XML document by traversing this tree. This is also the starting point for the XML document operation. We always get a Document object by parsing the XML source file, and then perform subsequent operations. In addition, the Document also includes methods for creating other nodes, such as CREATTRIBUT () to create an ATTR object. The main methods it contains: CreateAttribute (string): Create an Attr object with a given attribute name and can be placed on a ELEMENT object using the setAttributeEnode method. CreateElement: Create a Element object with a given tag name, representing a label in the XML document, then adding an attribute on this ELEMENT object or other actions. CreateTextNode (String): Create a Text object with a given string, and the Text object represents the plain text string included in the label or property. If there is no other label in a tag, the text object represented by the text within the tag is the unique sub-object of this ELEMENT object. GetElementsBytagname (String): Returns a NodeList object that contains a label for all given tag names. GetDocumentElement (): Returns a Element object that represents the root node of this DOM tree, which is the object representing the XML document root element. The Node object is the most basic object in the DOM structure, which represents an abstract node in the document tree. When actually use, rarely use Node objects, but use the sub-objects such as Element, Attr, Text, other Node objects to operate the document. The Node object provides an abstract, common root for these objects. Although the method of accessing its sub-node is defined in the Node object, there are some Node sub-objects, such as a Text object, which does not have a child node, which is to pay attention to. The main methods included in the Node object are: appendchild (org.w3c.dom.node): Add a child node to this node and placed in all child nodes, if this child has existed, first delete it first Drop it and add it. GetFirstChild (): If there is a child node, return the first child node, peer, and the getLastChild () method returns the last child node. Getnextsibling (): Returns the next brother node, peer, peer, and getPrevioussibling () method returns to its previous brothers node. GetNodeName (): Returns the name of the node based on the type of node. GetNodeType (): Returns the type of node. GetNodeValue (): Returns the value of the node. HaschildNodes (): Judging is that there is a child node. Hasattributes (): Judging whether this node has attributes. GetownerDocument (): Returns the Document object where the node is located. INSERTBEFORE (org.w3c.dom.node new, org.w3c.dom.node ref): Insert a child object before a given child object.
RemoveChild (Org.w3c.dom.Node): Delete a given child node object. Replacechild (org.w3c.dom.node new, org.w3c.dom.node ": instead of a given child node object with a new Node object. Nodelist objects, as the name suggestions, represents a list containing one or more NODEs. You can simply look into an array of NODEs, we can get the elements in the list by way: getLength (): Returns the length of the list. Item (int): Returns the Node object of the specified location. The ELEMENT object represents the label element in the XML document, inherits in Node, is also the most important child object of Node. In the label, you can include an attribute, so there is a method of accessing its properties in the Element object, and the method defined in any Node can also be used on the ELEMENT object. GetElementsBytagname (String): Returns a NodeList object that contains labels with a given tag name in the sub-fence nodes under this tab. GetTagname (): Returns a string representing this tag name. GetAttribute (String): Returns the value of the properties of the given property name in the tab. What is maintained here is that there should be an entity attribute to appear in the XML document, and this method does not apply to these entity properties. At this time, you need to use the GetAttributeEnodes () method to get an Attr object for further operations. GetAttributeNode (String): Returns an Attr object that represents a given property name. The AtTR object represents the properties in a tab. Attr is inherited in Node, but because Attr is actually included in ELEMENT, it does not be seen as the child object of Element, so attr is not part of the DOM tree in the DOM, so getParentNode (), getPrevioussibling () And getNextSibling () return will be NULL. That is, the attr is actually considered part of the ELEMENT object that contains its Element object, which does not appear as a separate node in the DOM tree. This is different from other Node sub-objects when used. It should be noted that the DOM object described above is defined in the DOM, which is defined when defined is the IDL language that is independent of the specific language when defined. Thus, the DOM can actually be implemented in any object-oriented language, as long as it implements the interface and functionality defined by the DOM. At the same time, some methods are not defined in the DOM, which is expressed with the properties of the IDL. When mapped to a specific language, these attributes are mapped to the corresponding method. Package testXML;
/ ** * *
Title: p> *
Description: * DOM's basic objects are 5: Document, Node, NodeList, Element, and Attr. * Document object represents the entire XML document, all other node, * all contained in the Document object in a certain order, arranged as a tree structure, * programmers can get XML documents by traversing this tree All content, this is also the starting point for the XML document operation. * We always get a Document object by parsing the XML source file, and then perform subsequent operations. * Furthermore, the Document also contains methods for creating other nodes, * such as CREATEATTRIBUT () is used to create an ATTR object. The main methods it contains: * p> *
* CreateAtTribute (string): Create an Attr object with a given attribute name, * and you can use the setattributenode method to place in a certain ELEMENT Object is top. * P> * * CreateElement (String): Create an Element object with a given tag name, * representing a tab in the XML document, then adding an attribute on this Element object or other operations. * * CreateTextNode (String): Create a Text object with a given string, * Text object represents the plain text string included in the label or property. If there is no other label in a tag, * The text object represented by the text in the tag is the unique sub-object of this Element object. * * GetElementsBytagname (string): Returns a NodeList object that contains a label for all given tag names. * * GetDocumentelement (): Returns a ELEMENT object that represents the root node of this DOM tree, * is also the object representing the XML document root element. * * *
Copyright: Copyright (c) 2005 p> *
Company: p> * @Author meConsea * @version 1.0 * / import java.io. *; Import java.util. * ;
Import javax.xml.parsers. *; import javax.xml.transform. *; import javax.xml.transform.dom. *; import javax.xml.Transform.Stream. *;
Import org.w3c.dom. *;
Public class myxmlreaderdom {
Public myxmlreaderdom () {}
public static void main (String args []) {// create xmlFile File file = new File ( "f: //testXml//link.xml"); try {// create DocumentBuilderFactory DocumentBuilderFactory bdf = DocumentBuilderFactory.newInstance (); // Create DocumentBuilder DocumentBuilder DB = bdf.newDocumentBuilder (); // Create Document Document Doc = DB.PARSE (file); // Clean XML DOC as a formatted blank, and unnecessary Text Node in DOC tree Object Doc.Normalize (); / ** * Use the Document object's getElementsBytagname () method, * We can get a NodeList object, a Node object represents a tag element in an XML document, * and Nodelist objects, view their name And it is known, the representative is a list of Node objects * / nodelist links = doc.getlementsBytagname ("link"); // Clean Blank. You can use getElementBytagname (string) for ELEMENT for (int i = 0; i / ** * After a Node object is created, the data saved in the XML document is extracted and packaged in this node. * In this example, to extract the content within the Message tag, we usually use the getNodeValue () method of the Node object: * / System.out.println ("text" link.getlementsBytagname ("text"). Item (0) .GetfirstChild (). GetnodeValue (); system.out.println ("URL" link.getlementsBytagname ("URL" ) .item (0) .Getfirstchild (). getnodeValue (); system.out.println ("author" link.getlementsBytagname ("author"). item (0) .GetfirstChild (). getnodeValue ()); system .out.println ("date:"); // Date exists Element Linkdate = (element). Item (0); system.out.print ("day:" linkdate. getElementsBytagname ("day"). Item (0) .GetfirstChild (). getnodeValue ()); System.out.Print (": Month" linkdate.getElementsBytagname ("Month"). Item (0) .GetfirstChild (). GetNodeValue ()); system.out.print (": year:" linkdate.getElementsBytagname ("year"). item (0) .GetfirstChild (). getnodeValue ()); system.out.println ("description" Link.getElementsBytagname ("description"). item (0) .GetfirstChild (). getnodevalue ());} // Write XML // Data Source IO / JDBC / etc String text = "test xml"; string url = "http://3w.test.xml"; string author = "meconsea"; string description = "test xml write"; Text textseg; element linknew = doc.createElement ("link"); Element linktext = doc.createElement ("text"); textseg = doc.createtextNode (text); linktext.Appendchild (textseg); linknew.Appendchild (LinkText); Element linkUrl = doc.createElement ( "url"); textseg = doc.createTextNode (url); linkUrl.appendChild (textseg); linkNew.appendChild (linkUrl); Element linkAuthor = doc.createElement ( "author"); textseg = doc .CreateTextNode (Author); LinkAuthor.Appendchild (TextSeg); Linknew.Appendchild (LinkAuthor); java.util.Calendar rightNow = java.util.Calendar.getInstance (); String day = Integer.toString (rightNow.get (java.util.Calendar.DAY_OF_MONTH)); String month = Integer.toString (rightNow.get (java .util.calendar.month); string year = integer.tostring (rightnow.get (java.util.calendar.year); Element LinkData = Doc.createElement ("DATA"); Element linkDataDay = doc.createElement ("day"); textseg = doc.createtextNode (day); linkDataDay.Appendchild (textseg); linkData.Appendchild (LinkDataDay); Element LinkDataMonth = Doc.createElement ("MONTH"); TextSeg = Doc.createTextNode (Month); linkDataMonth.Appendchild (textseg); linkData.Appendchild (LinkDataMonth); Element linkDatarayear = doc.createElement ("year"); textseg = doc.createtextNode (year); linkDataEar.Appendchild (TextSeg); linkData.Appendchild (LinkDataMonth); Linknew.appendchild (linkdata); Element linkDescription = doc.createElement ("description"); textseg = doc.createtextNode (description); linkDescription.Appendchild (textseg); linknew.appendchild (linkdescription); // Add doc Tree Doc.getdocumentelement (). appendchild (linknew); // out by xslt TransformerFactory tf = TransformerFactory.newInstance (); Transformer tformer = tf.newTransformer (); DOMSource dos = new DOMSource (doc); StreamResult result = new StreamResult (file); tformer.transform (dos, result); } catch (exception e) {system.out.println ("Exception E" E.GetMessage ()); E.PrintStackTrace ();}} }