JAVA Programming update the common methods of XML documents
This paper briefly discussed the updated Java programming language in XML documents of the four common methods, and analysis of the pros and cons of these four methods. Secondly, the paper also Java programs on how to control the output of the XML document format on the start.
JAXP is Java API for XML Processing the first initials of the word, the Chinese meaning is: for the use of XML documents dealing with the preparation of the Java programming language interface. JAXP supports DOM, SAX, XSLT standards. In order to enhance the flexibility of JAXP use, development, particularly for the design of a JAXP Pluggability Layer, in Pluggability Layer support, and specific JAXP can achieve DOM API, SAX API of XML parser (XML Parser, for example, Apache Xerces) joint work, but also the implementation of XSLT and specific standards XSLT processor (XSLT Processor, such as Apache Xalan) joint work. Application of the benefits is Pluggability Layer: JAXP we only need to know the definition of the various programming interface can be, without the need for the concrete used in the XML parser, XSLT processors have a deeper understanding. For example, in a Java program, called by JAXP XML parser Apache Crimson processing of XML documents, if we wish to use other XML parser (such as Apache Xerces), in order to improve the performance of the procedure, then the original code may does not warrant any change, can be used directly (you need to do is to include Apache Xerces code jar files into the CLASSPATH environment variable, and will include Apache Crimson code jar files in the CLASSPATH environment variable deleted).
Currently JAXP application has been very common, can be said to be dealt with in the Java language XML document standard API. Some beginners in learning to use the process of JAXP often asked the question: I prepared for the procedure done a DOM Tree update, but when exit procedures after the original XML documents and has not changed, Fushilaoyangzi, how to achieve XML documents and the original synchronous updates DOM Tree? yesterday a view, the JAXP did not appear to provide the necessary interfaces / methods / category, and many beginners are confused about the issue. The thrust of this paper is to address this problem, a brief introduction of several commonly used simultaneously update the original XML documents and DOM Tree method. In order to narrow the scope of the discussion, the paper involved in the XML parser only include Apache Crimson and Apache Xerces, and XSLT processor used only Apache Xalan.
Method 1: direct read and write XML documents
This is perhaps the most stupid most primitive solution. When DOM Tree acquisition procedures, the application of DOM Node Interface model the various methods on DOM Tree updated, the next step should be to the original XML documents updated. We can use recursive methods or application TreeWalker category, traversing the entire DOM Tree at the same time, DOM Tree each node / write element in the pre-opening of the original XML document, be times when DOM Tree Calendar completely, DOM Tree and the original XML document on the realization synchronous updates. In reality, this method rarely used, but if you want to realize their own programming XML parser, which means it is still possible to the useful.
Method 2: Use XmlDocument class
Using XmlDocument class? JAXP not clear in this class! Author is not wrong? Not wrong! Category is the use of XmlDocument, exact, the use of XmlDocument class write () method.
Already mentioned above, can JAXP XML parser and a variety of joint use, we selected the XML parser is Apache Crimson. XmlDocument (org.apache.crimson.tree.XmlDocument) Apache Crimson is a category not included in the standard JAXP, no wonder the documents in the JAXP XmlDocument class invitation and could not find it. Out of the question now, how to achieve application XmlDocument class update XML documents? XmlDocument in the category provided the following three write () method (based on the latest version of Crimson —— Apache Crimson 1.1.3):
Public void write (OutputStream out) throws IOException
Public void write (Writer out) throws IOException
Public void write (Writer out, String encoding) throws IOException
The three write () method is the primary role of DOM Tree output of the contents of the output to a specific medium, such as paper output streams, application console, and so on. So how can the use of these three write () method? Look at the Java code fragment:
String name = "fancy";
DocumentBuilder parser;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ();
Try
(
Factory.newDocumentBuilder parser = ();
Document doc = parser.parse ( "user.xml");
Element newlink = doc.createElement (name);
Doc.getDocumentElement (). AppendChild (newlink);
((XmlDocument) doc). Write (new FileOutputStream (new File ( "xuser1.xml ")));
)
Catch (Exception e)
(
/ / To log it
)
In the above code, the first creation of a Document Object doc, for a complete DOM Tree, and then Application Node Interface appendChild () method, in the final DOM Tree has been added as a new node (fancy), and finally call XmlDocument class write (OutputStream out), the DOM Tree output to the contents of xuser.xml (in fact can also output to user.xml, updating the original XML documents, in order to facilitate done here compared Therefore, the paper output to xuser.xml ). Needs attention is not directly on the Document Object doc called directly write () method because JAXP Document Interface and the absence of a definition of any write () method, it must be mandatory doc from the Document Object converted to XmlDocument object, and then call to write ( ), in the above code is used in the write (OutputStream out) method, this method using the default UTF-8 encoded output DOM Tree specific to the content of the output medium, if DOM Tree contains Chinese characters, then output of results may be garbled, which is the existence of so-called "Chinese characters", a solution is to use write (Writer out, String encoding), Explicit designated output encoding, such as the second parameter set to " GB2312 ", which does not exist at this time," Chinese characters ", the output results show that normal Chinese characters.
Complete examples refer to the following documents: AddRecord.java (see Annex), user.xml (see annex). The example of the operation of the environment: Windows XP Professional, JDK 1.3.1. In order to compile the normal operation AddRecord.java this procedure, you will need to download the website http://xml.apache.org/dist/crimson/ Apache Crimson, and documents obtained crimson.jar joined in the CLASSPATH environment variable.
NOTE:
Apache Crimson is the predecessor of Sun Project X Parser, but I do not know why, X Parser evolved into Apache Crimson, Apache Crimson since many of the code are directly from the X Parser the transplant. For example used above the XmlDocument class, it is com.sun.xml.XmlDocument X Parser, in the Apache Crimson suddenly, it becomes a org.apache.crimson.tree.XmlDocument category, in fact the vast majority of them code is the same as the package may import statements, as well as statements and the beginning of the document section lience be different. Early JAXP and is bundled with the X Parser, some old procedures used com.sun.xml package, if you recompile them, may not be passed, certainly because of this reason. Later, the JAXP and Apache Crimson tied together, such as JAXP 1.1, if you use JAXP 1.1, so no additional download Apache Crimson, but also the normal operation of the above example compiler (AddRecord.java). As JAXP 1.2 EA (Early Access) to change its ways, a better performance by Apache Xalan and Apache Xerces respectively as XSLT processor and the XML parser, and can not directly support the Apache Crimson, so if you use a development environment or JAXP 1.2 EA is Java XML Pack (includes JAXP 1.2 EA), it will not be able to run directly compile the example above (AddRecord.java), you need to download and install additional Apache Crimson.
Tags: java documents, java programming ideas, java programming language, java xml






