Java update XML four common ways

zhaozj2021-02-16  108

This article briefly discusses four common methods for updating XML documents in Java language program, and analyzes the advantages and disadvantages of these four methods. Secondly, this article also made the format of how to control the XML document output by the Java program. JAXP is the English header abbreviation of Java API for XML Process, which is the programming interface written using Java language for XML document processing. JAXP supports DOM, SAX, XSLT and other standards. In order to enhance the flexibility in JAXP, developers have designed a PlugGability Layer for JAXP. Under the support of Pluggability Layer, JAXP can implement the DOM API, SAX API's various XML parsers (XML Parser, for example Apache Xerces) Joint work, and the XSLT processor (XSLT Processor, such as Apache Xalan), which is specifically implemented. The benefit of applying Pluggability Layer is that we only need to be familiar with the definition of JAXP's respective programming interfaces, without having to understand the specific XML parsers used, the XSLT processor has a deep understanding. For example, in a Java program, the XML parser Apache Crimson is called via JAXP to process the XML document, if we want to use other XML parsers (such as apache Xerces) to increase the performance of the program, then the original program code may not Need to change, you can use it directly (what you need to do is just JAR files containing the Apache Xerces code to the environment variable ClassPath, and the JAR file containing the Apache Crimson code is removed in the environment variable ClassPath). At present, JAXP has been applied very common, which can be said to have a standard API for processing an XML document in a Java language. Some beginners are learning such a problem in learning using JAXP: I have written updates to the Dom Tree, but when the program exits, the original XML document does not change, or the old look, how to implement it The original XML document and the Synchronization update of the DOM Tree? At first, there seems to be no corresponding interface / method / class in JAXP, which is a problem that many beginners are confused. The main purpose of this article is to solve this problem, simply introduce several commonly used synchronous update of the original XML document and the Dom Tree method. In order to narrow the scope of the discussion, the XML parsers involved herein include only Apache Crimson and Apache Xerces, while the XSLT processor only uses Apache Xalan. Method 1: Direct reading and writing XML documents this may be the most stupid and most original way. After the program acquires the DOM Tree, each method of the Node interface to the DOM model is updated, and the next step should be updated the original XML document. We can use the recursive approach or to apply the TreeWalker class. Traverse the entire Dom Tree, while writing every node / element of the Dom Tree, in a pre-opened original XML document, when the DOM Tree is traveled, Dom Tree and the original XML documentation implement synchronous updates. In practice, this method is rarely used, but if you want to program your own XML parser, this method is still possible to use.

Method 2: Using the XMLDocument class using the XMLDocument class? Dividing this class in JAXP! Is the author mistaken? Nothing! That is to use the XMLDocument class, it is a Write () method using the XMLDocument class. The above has been mentioned above, JAXP can be used in combination with a wide variety of XML parsers, this time we choose the XML parser is apache crimson. XMLDocument (org.apache.crimson.tree.xmldocument) is a class of Apache Crimson, not included in the standard JAXP, can't find the XMLDocument class in the JAXP documentation. Now the problem came out, how to apply the XMLDocument class to implement the function of updating the XML document? The following three write () methods are available in the XMLDocument class (the latest version of CRIMSON ---- Apache Crimson 1.1.3):

The main role of public void write (OutputStream out) throws IOExceptionpublic void write (Writer out) throws IOExceptionpublic void write (Writer out, String encoding) throws IOException above three write () method is that the contents of the DOM Tree output to a specific output medium In this, such as file output flow, application console, etc. So how do you use the above three Write () methods? Please see the following Java program code snippet:

String name = "fancy"; DocumentBuilder parser; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); try {parser = factory.newDocumentBuilder (); Document doc = parser.parse ( "user.xml"); Element newlink = doc.createElement ( Name); doc.getdocumentelement (). appendchild (newlDocument); (new fileoutputstream (new fileoutputstream (new file ("xuser1.xml"))));} catch (Exception E) {// to log it }

In the above code, first create a Document object DOC, get the complete DOM Tree, then apply the appendchild () method of the Node interface, and add a new node in the DOM Tree, finally call the XMLDocument class Write (OUTPUTSTREAM OUT) Method, output content in the DOM Tree into xuser.xml (actually output to user.xml, updating the original XML document, here for easy contrast, so output to XUSER.XML files) . It should be noted that the WRITE () method is directly called directly to the Document object DOC, because the JAXP's Document interface does not define any Write () method, so you must force the DOC to convert the Document object to the XMLDocument object, and then call Write () Method, in the above code is the Write (OutputStream out) method, this method uses the default UTF-8 encoded output DOM Tree to a specific output medium, if the Chinese characters are included in the DOM Tree, then output The result may be garbled, that is, there is so-called "Chinese character issues", the solution is to use the Write (Writer Out, String Encoding) method, explicitly specify the encoding, such as setting the second parameter "GB2312 ", At this time, there is no" Chinese character problem ", and the output results can display Chinese characters normally. For a complete example, please refer to the following files: addRecord.java (see attachment), user.xml (see attachment). The operating environment of this example is: Windows XP Professional, JDK 1.3.1. To be able to run the addRecord.java, you need to download Apache Crimson to the URL http://xml.apache.org/dist/crimson/ and add the acquired crimson.jar file to the environment variable ClassPath. Note: Apache Crimson's predecessor is Sun Project X Parser, and later I don't know why, the X Parser evolved into Apache Crimson, and many of the code of Apache Crimson has been directly transplanted from X Parser. For example, the XMLDocument class used above, it is com.sun.xml.xmldocument in X Parser, and it has become an org.apache.crimson.tree.xmldocument class, in fact they have The code is the same, it may be different from the package statement and the import statement, and the beginning of the file. Early JAXP is bundled with X Parser, so some old programs use com.sun.xml package, if you recompile them now, it is possible to pass, it is definitely because of this reason. Later JAXP and Apache Crimson bundle together, such as JAXP 1.1, if you use JAXP 1.1, then you don't need to download Apache Crimson, you can also properly compile the example (AddRecord.java). The latest JAXP 1.2 EA (Early Access) is changing the string, using performance better Apache Xalan and Apache Xerces as the XSLT processor and XML parser, can not directly support Apache Crimson, so if your development environment uses JAXP 1.2 EA or It is Java XML PACK (including JAXP 1.2 EA), then you will not be able to directly compile the above example (AddRecord.java), you need to download and install Apache Crimson.