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. Specially encapsulated the following public methods:
1. Document object creation (including empty Document object creation, is created as a reference node node as root node.
2. Convert a specified XML string into a Document object.
3. Read an XML file from a physical hard drive 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 (such as the necessary DOM implementatory parser and other packets without adding the environment), so it is only very simple when packaged. Get its Message throw.
code show as below:
/ **
* Initialize an empty Document object to return.
* @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 ();
}
/ **
* Initialize a DocumentBuilderFactory * @return a documentbuilderFactory
* /
Public Static DocumentBuilderFactory NewDocumentBuilderFactory () {
DocumentBuilderFactory DBF = DocumentBuilderFactory.newinstance ();
DBF.setNamespaceaware (TRUE);
Return DBF;
}
/ **
* Convert the incoming XML String 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 ());
}
}
/ **
* Given an 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 (exception e) {
Throw new runtimeException (E.GetMessage ());
}
}
/ **
* Given a file name to get the file and resolve it to an org.w3c.dom.Document object returns.
* @Param filename file name to be parsed
* @Return a org.w3c.dom.document
* /
Public Static Document LoadxmldocumentFromFile (String filename) {
IF (filename == NULL) {
Throw New IllegalargumentException ("No File Name and Its Physical Path!");
}
Try {
Return NewDocumentBuilder (). PARSE (New File (FileName);
} catch (saxException e) {
Throw new IllegalargumentException
"The target file (" filename ") cannot be correctly resolved to XML! / N" E.getMessage ());
} catch (ioexception e) {
Throw new IllegalargumentException
"You can't get the target file (" FileName ")! / N" E.getMessage ());} catch (ParserConfigurationExcection) {
Throw new runtimeException (E.GetMessage ());
}
}
/ **
* Given a node, add the node to the new structure of the document.
* @Param Node a Document Node
* @Return a new document
* /
Public static document new xmldocument (node node) {
Document doc = newxmldocument ();
Doc.Appendchild (Doc.ImportNode (Node, true);
Return DOC;
}
/ **
* Output of 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!");
}
/ **
* Output of 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!");
}
/ **
* Get a Transformer object, which is extracted as a public method due to the same initialization because it is used.
* @Return a Transformer Encoding GB2312
* /
Public static transformer newTransformer () {
Try {
TRANSFORMER TRANSFORMER =
TRANSFORMERFAACTORY.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.setputputProperties (Properties);
Return Transformer;
} Catch (TransformerConfigurationException TCE) {
Throw new runtimeException (tce.getMessage ());
}
}
/ **
* Returns an error message of a paragraph XML. The title of the prompt is: system error. The reason why the use of string assembled, mainly
* There will be no abnormality.
* @Param errmsg prompt error message
* @return a xml string show err msg
* /
Public static string erroMlstring (string errmsg) {
StringBuffer MSG = New StringBuffer (100);
msg.append (" XML Version = /" 1.0 / "encoding = /" GB2312 / "?>");
Msg.Append ("
Return msg.tostring ();
}
/ **
* Returns an error message of a paragraph XML. Tips for the message is: System error
* @Param errmsg prompt error message
* @Param Errclass throws the error class for extracting the wrong source information.
* @return a xml string show err msg
* /
Public static string errxmlstring (string errmsg, class errclass) {
StringBuffer MSG = New StringBuffer (100);
msg.append (" XML Version = /" 1.0 / "encoding = /" GB2312 / "?>");
Msg.Append
" Errmsg "/" errsource = / "" Errclass.getName () "/" /> "); Return msg.tostring (); / ** * Returns an error message of a paragraph XML. * @Param Title Tips Title * @Param errmsg prompt error message * @Param Errclass throws the error class for extracting the wrong source information. * @return a xml string show err msg * / Public Static string errxmlstring String Title, String Errmsg, Class Errclass) { StringBuffer MSG = New StringBuffer (100); msg.append (" XML Version = /" 1.0 / "encoding = /" GB2312 / "?>"); Msg.Append " Title "/" errmsg = / "" Errmsg "/" errsource = / "" Errclass.getName () "/" /> "); Return msg.tostring (); } 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 needs get a node text information, as follows XML nodes: TEXT element> When you have an Element Node Object, you want to get the text information "text", first get the sub-node list of the Element node, to determine if there is a child node, if you exist, then you find a TextNode node, through GetNodeValue () Method to obtain this text information, since there is no sub-node when the Element node does not have information, it is necessary to determine whether the ELEMENT node has a child node to access the TextNode node that truly contains text information, then if the data to be processed is This form is given, it will increase a large number of development code, which makes the development work boring, so this uses a default agreement implementation, that is, a public method is given, the method takes a given Node direct child node Text node text information, if there is no TEXT node, returns NULL, although the usage of this method is limited, it can also cause an error to use this method, but according to the actual use, this convention and use The way is no problem, because the actual use is the situation of the example above, the code: / ** * This method gets the Text Node text information 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 String If the given node exists in the Text child node, return the first access child node text message, if there is no existence, return NULL. * / Public static string getnodevalue (node node) { IF (node == null) {Return NULL; } Text text = getTextNode (Node); IF (Text! = null) { Return text.getnodeValue (); } Return NULL; } / ** * This method gets the 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 IF (List.Item (i) .Getnodetype () == node.text_node) { Return (Text) List.Item (i); } } } Return NULL; } The above code will be packaged separately from the direct Text child node of a given Node node. 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 starting with the direct child node under the root node. 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 to search for nodes * @Param Tagname to search for label name * @Return A ELEMENT Gets the first node of the node list of tagname as the label name. * / Public Static Element GetFirstElementByName Element Element, String tagname) { Return (Element) getFirstlement (Element.getElementsBytagname); } / ** * Get the first node from a given node list, return null if the node is set to null / empty. * @Param nodelist a nodelist * @Return a node * / Private static node getfirstlement (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 have no direct positioning access method. Also, because DOM2 specification is caused, the DOM2 specification cannot be added directly to the child node of another Document Docb object. To do this, you must first pass DOCA nodes to DOCB. After the ImportNode method transitions, in the sub-node list added to the target node, there is also a method to handle: / ** * This method attaches the root node of the parameter appendedDoc and the following nodes 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 (appendeddoc! = null) { Doc.getfirstchild (). Appendchild ( Doc.ImportNode (appendeddoc.getfirstchild (), true)); } } / ** * This method attaches the root node of the parameter appendedDoc and the following nodes below to the Node node. * The post-made child node as the Node node. * Equivalent to: node.appenddoc (appendednode); * @Param Node The node to be added will be added to the end 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; } 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.