Document Object Model (DOM) is a document standard that provides a lot of flexibility for complete documents and complex applications. The DOM standard is standard. It is very strong and complete, and there are many implementations. This is a large-scale decision factor-especially for product applications to avoid a lot of rewriting when the API changes. The above is the reason why I did not select other object-oriented standards such as JDOM or DOM4J when choosing to process XML data, but because DOM starts from the beginning, it is a model-independent model, and it is more trend to be used in c or Perl This language does not use Java's object-oriented performance, so there is a lot of trouble during use, and today it is a summary. In addition, I am currently using XML mainly as a unified format of data transmission, and the interface displayed by the user interface is not very wide, so the content of the DOM used is not much. When preparing to use it, it is a sufficient preparation, and there is also a difficult preparation, so there is a simple tool class to encapsulate the necessary public methods when using the DOM object, actually prove that this is very Wise, a simple creation of the Document object, if you need to write 5 lines of code every time, it is necessary to handle those annoying Exception, it will hit everyone's enthusiasm, so in the initial, I made an XMLTool class. The following public methods are packaged: 1. Document object creation (including empty Document object creation, creating a given Node node as root node. 2, converting a specified XML string into a document object .3, Read an XML file from a physical hard disk and return a Document object. 4. Convert a Node object into a string.
Each method intercepts the abnormality thrown by the associated DOM operation, converts into a runtimeException, and these exceptions are in the actual use process, especially when generating a Document object, especially when generating a document ParserConfigurationException, Transfer Node Node When the TRANSFORMERCONFIGURATIONEXCEPTION is generated when the TRANSFORMER object is generated, and there is no need to spend time energy on them. Moreover, it has really been related, in fact, there is no way to deal with it. This situation is usually a problem with the system environment. Get its Message throw. The code is as follows: / ** * Initialize an empty Document object returns. * @Return a Document * / public static Document newXMLDocument () {try {return newDocumentBuilder () newDocument ();.} Catch (ParserConfigurationException e) {throw new RuntimeException (e.getMessage ());}}
/ ** * initialize a DocumentBuilder * @return a DocumentBuilder * @throws ParserConfigurationException * / public static DocumentBuilder newDocumentBuilder () throws ParserConfigurationException {return newDocumentBuilderFactory () newDocumentBuilder ();.}
A / * ** incoming / ** * initialize a DocumentBuilderFactory * @return a DocumentBuilderFactory * / public static DocumentBuilderFactory newDocumentBuilderFactory () {DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance (); dbf.setNamespaceAware (true);; return dbf} XML string is converted into an org.w3c.dom.Document object returns. * @Param Xmlstring A string expression that meets the XML specification. * @Return a Document * / public static Document parseXMLDocument (String xmlString) {if (xmlString == null) {throw new IllegalArgumentException ();} try {return newDocumentBuilder () parse (new InputSource (new StringReader (xmlString))). ;} Catch (exception e) {throw new runtimeException (E.getMessage ());}}
/ ** * give a input stream, resolve it to an org.w3c.dom.Document object returns. * @Param input * @return a org.w3c.dom.Document * / public static Document parseXMLDocument (InputStream input) {if (input == null) {throw new IllegalArgumentException ( "parameter is null!");} Try {return NewDocumentBuilder (). parse (input);} catch (e.) {throw new runtimeException (E.GetMessage ());}} / ** * gives a file name to get the file and resolve to an org.w3c. The Dom.Document object returns. * @Param fileName file name * @return a org.w3c.dom.Document * / public static Document loadXMLDocumentFromFile (String fileName) {if (fileName == null) file to be parsed {throw new IllegalArgumentException ( "filename is not specified and Its physical path! ");} Try {return newDocumentbuilder (). Parse (new file (saxception e) {throw new illegalgumentException (" Target file (" filename ") cannot be correctly resolved to XML! / N " E.getMessage ());} catch (ioException e) {throw new illegalargumentException (" You cannot get the target file (" filename ")! / N " E.getMessage ());} catch (ParserConfigurationException E) {throw new runtimeException (E.GetMessage ());}} / ** * Given a node to add this node to the new structure of the Document. * @Param node a Document node * @return a new Document * / public static Document newXMLDocument (Node node) {Document doc = newXMLDocument (); doc.appendChild (doc.importNode (node, true)); return doc;}
/ ** * Outsourcing a DOM Node object into a string. Returns an empty string "" if it fails. "". * @Param Node Dom Node object. * @Return a XML String from node * / public static String toString (Node node) {if (node == null) {throw new IllegalArgumentException ();} Transformer transformer = newTransformer (); if (! Transformer = null) {try {StringWriter sw = new StringWriter (); transformer.transform (new DOMSource (node), new StreamResult (sw)); return sw.toString ();} catch (TransformerException te) {throw new RuntimeException (te.getMessage ()) }} Return errxmlstring ("You cannot generate XML information!");} / ** * outputs the incoming DOM Node object into strings. Returns an empty string "" if it fails. "". * @Param Node Dom Node object. * @Return a XML String from node * / public static String toString (Node node) {if (node == null) {throw new IllegalArgumentException ();} Transformer transformer = newTransformer (); if (! Transformer = null) {try {StringWriter sw = new StringWriter (); transformer.transform (new DOMSource (node), new StreamResult (sw)); return sw.toString ();} catch (TransformerException te) {throw new RuntimeException (te.getMessage ()) ;
}}}} RETURN ERRXMLSTRING ("You cannot generate XML information!");} / ** * Get a Transformer object, because of the same initialization, so it is extracted as a public method. * @Return a Transformer encoding gb2312 * / public static Transformer newTransformer () {try {Transformer transformer = TransformerFactory.newInstance () newTransformer ();. Properties properties = transformer.getOutputProperties (); properties.setProperty (OutputKeys.ENCODING, "gb2312 "); properties.setProperty (OutputKeys.METHOD," xml "); properties.setProperty (OutputKeys.VERSION," 1.0 "); properties.setProperty (OutputKeys.INDENT," no "); transformer.setOutputProperties (properties); return TRANSFORMER;} Catch (TransformerConfigurationException TCE) {throw new runtimeException (tce.getMessage ());}} / ** * Returns an error message for XML expressed. The title of the prompt is: system error. The reason why the use of string is mainly, it is mainly to do it. * There will be no abnormality. * @Param errmsg Tip Error message * @Return A XML string show err msg * / public static string errxmlstring (string errmsg) {StringBuffer msg = new stringbuffer (100); msg.append (" Xml version = /" 1.0 / "eNCoding = /" GB2312 / "?>"); msg.append ("
* @Return a XML String show err msg * / public static String errXMLString (String errMsg, Class errClass) {StringBuffer msg = new StringBuffer (100); msg.append ( "?
The above is the basic application of the DOM, so it will not explain it in detail. In the actual use process, there are several status to use very frequently, but the design of the DOM interface makes the operation cumbersome, so the corresponding processing method is added. Among them, the most troublesome need gets the text information of a node. The following XML node:
IF (Text! = null) {returnization text.getnodeValue ();
Return NULL;
/ ** * This method gets a TEXT node under a given Node, and returns NULL if there is no Text node. * Note: It is a direct child node, a phase difference 2 or 2 floors will not be considered. * @Param node a node A node. * @Return a text If the given node exists, returns the first access child node, and returns NULL if there is no existence. * / Public static text getTextNode (node node) {if (node == null) {return null;} if (node.haschildnodes ()) {nodelist list = node.getChildNodes (); for (int i = 0; i < List.getLength (); i ) {if (list.item (i) .Getnodetype () == node.text_node) {return (text) List.Item (i);}}} return null;}}} Return Null;} The above code will be obtained The direct Text child node for a given Node node is separated.
Another situation often encounters, I want to directly locate the target node, get the node object, without the need to find the target node through a layer of node, and at least the DOM2 interface provides at least the following way positioning Node: 1. For Document Objects: 1) GetDocumentElement () - Get root node objects, actually useful, because the root node is basically just root node, the actual data node is the direct child node under the root node Start. 2) getElementByid (String Elementid) - This method should be a best positioning method, but it is not used in the actual use, the main reason is that "ID" is different from the attribute of a node "ID" ID ", This is clearly pointed out in the API description of org.w3c.dom.document, and I haven't seen a lot of information, so I have to give up. 3) getElementsBytagname (String Tagname) - This method is actually there is no way to choose, but it has to be used, but it is also very suitable, although the method returns a nodelist, but actually uses the tagname of the node to design Special string, then you can get it directly, and actually use, it is also almost the same. Many times take the field names in the database as Tagname to facilitate the value of this field, in a simple agreement, Used the following method: / ** * This method retrieves all tagnames under the parameter ELEMENT as: tagname, and returns the first node of the node list. * Returns NULL if there is no node of the tagname. * @Param Element Search Node * @Param Tagname Search Tagname * @return A ELEMENT gets the first node for the node list of tagname as the label name. * / Public static Element getFirstElementByName (Element element, String tagName) {return (Element) getFirstElement (element.getElementsByTagName (tagName));} / ** * get the first node list returned from the given node, if the node is set NULL / empty, return NULL. * @Param nodeList a NodeList * @return a Node * / private static Node getFirstElement (NodeList nodeList) {if (nodeList == null || nodeList.getLength () == 0) {return null;} return nodeList.item (0 );} This agreement seems to be limited, in fact, this is basically like this, just get the first give Tagname's ELEMENT node. 4) getElementsBytagnamens (String Namespaceuri, String localname) - This method is basically not used because there is no status that needs to use namespaces.
2, for the Element object ---- Element object and Document objects, less getDocumentElement () method, but it is mainly using the getElementSbyTagname () method as with Document. 3. Other node objects There is basically no direct positioning access method, because DOM2 specification is caused by the DOM2 specification, and the Node of Document Doca cannot directly join the node of another document docb object. In the middle, you must first turn the DOCA node through the DOCB's ImportNode method to the sub-node list of the target node, so there is a method to unify: / ** * This method will use the root node of the parameter appendeddoc and The following nodes are attached to the DOC's follow-up node. * As a sub-node for the working with the Node of the DOC. * Equivalent to: doc.appendDoc (appendedDoc); * @param doc a Document * @param appendedDoc a Document * / public static void appendXMLDocument (Document doc, Document appendedDoc) {if {doc.getFirstChild () (appendedDoc = null!) .appendchild (appendeddoc.getfirstchild (), true));}} / ** * This method attaches the root node of the parameter appendeddoc and the following nodes below the Node node. * The post-made child node as the Node node. * Equipped with: node.appendddoc (appendedNode); * @Param Node The node to be added will be added to the last of the node. * @Param appendedNode a node This node will be added as the last child node of the Node node. * / Public static void appendXMLDocument (Node node, Node appendedNode) {if (appendedNode == null) {return;} if (appendedNode instanceof Document) {appendedNode = ((Document) appendedNode) .getDocumentElement ();} node.appendChild ( Node.getownerDocument (). ImportNode (AppendedNode, True);
Basically, these are common, there are other fragmented methods, but they are not intended to be introduced. What is going to say is that if is the specific and easy access to the specific and easy access to the getElementById () method, please advise.