JSP and XML combination (2)

xiaoxiao2021-03-14  198

How to use JSP to develop DOM applications? DOM is the abbreviation of Document Object Model, that is, document object model. XML will organize the data into a tree, so the DOM is the object of this tree. It is a popular saying that by parsing the XML document, the XML document is logically established a tree model, and the node of the tree is an object. We can access the contents of the XML document by accessing these objects.

Let's take a look at a simple example and see how we do an XML document in the DOM. This is an XML document, and it is also the object we have to operate:

Good-bye Serialization, Hello Java!

Below, we need to resolve the content of this document into a Java object to go to the program, using JAXP, we can do this only one line of code. First, we need to build a parser factory to use this factory to get a specific parser object: DocumentBuilderFactory dbf = documentbuilderfactory.newinstance (); We are here using DocumentBuilderFacotry to create a program that is not related to the specific parser, When the static method of the DocumentBuilderFactory class is called, it determines which parser that is specifically used according to a system variable. Also because all parsers are obeyed from the interface defined by JAXP, the code is the same regardless of which parser is used. Therefore, when switching between different parsers, only the value of the system variable is required without changing any code. This is the benefits of the factory. DocumentBuilder DB = dbf.newdocumentBuilder (); After obtaining a factory object, use its static method NewDocumentBuilder () method to get a DocumentBuilder object, which represents a specific DOM parser. But which parser, Microsoft or IBM is not important for the program. We can then use this parser to parse the XML document: Document Doc = DB.PARSE ("C: /XML/Message.xml"); 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. Start with the obtained Document object, we can start our DOM trip. With the getElementsBytagName () method of the Document object, we can get a NodeList object, a Node object represents a label element in an XML document, and the Nodelist object, knows its name, which is representative of a node object. List: nodelist nl = doc.getElementsBytagname ("message"); we got through such a statement is a list of all tags in the XML document. We can then use the NodeList object's item () method to get every Node object in the list: node my_node = nl.Item (0); after a Node object is created, the data saved in the XML document is extracted. Come out and package it in this node. In this example, to extract the content within the Message tag, we usually use the getNodeValue () method of the Node object: string message = my_node.getfirstchild (). GetnodeValue (); Please note that a getFirstChild () method is also used here. Come get the first child Node object below the Message.

Although there is no other child tag or attribute outside the Message tag, we insist on using the getFirseChild () method, mainly related to the W3C's definition of the DOM. W3C defines the text section in the tag into a node, so we must get the Node representing the text, we can use getNodeValue () to get the text content. Now, since we have been able to extract data from the XML file, we can use these data in a suitable place to construct an app. Dom instance tells this example to do what is going to do, we save some URL addresses in a named link.xml file, we hope to read these URLs and displayed through the DOM, or in turn This XML file is written to the join URL address. It is very simple, but it is very practical, and it is also enough to exemplify the most of the DOM. The first program we call XMLDisplay.java, the main function is to read the contents of each node in this XML file, and then on the format output on System.Out, let's take a look at this program: import javax.xml.parsers IMPORT Org.w3c.dom. *;

This is an introduction of the necessary class because the XML parser provided here is required, thus need to introduce the Java.xml.Parsers package, which contains specific implementations of the DOM parser and SAX parsers. The org.w3c.dom package defines the DOM interface set by W3C. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder (); Document doc = builder.parse ( "links.xml"); doc.normalize ();

In addition to what to say, there is a small trick, call Normalize () to the Document object, which can remove blank as a formatted content in the XML document to map unnecessary text node objects in the DOM tree. Otherwise, the DOM tree you get may not be as you think. Especially when the output is output, this Normalize () is more useful.

Nodelist Links = Doc.GetElementsBytagname ("link");

Just said, the blank characters in the XML document will also be mapped in the DOM tree as an object. Thus, the getChildNodes method that directly calls the Node method sometimes some problems, sometimes it is possible to return the desired NodeList object. The solution is to use the Element's getElementBytagname (String), and the Nodelise returned is the object you are looking forward to. Then, you can extract the desired elements with item () methods.

For (int i = 0; i

The above code snippet completed the formatting output of the XML document content. As long as you pay attention to some details, such as the use of the GetFirstchile () method and the getElementsByTagname () method are easier. The following content is the problem that the DOM tree is revoked to the XML documentation after modifying the DOM tree. This program name is XMLWRITE.JAVA. In the JAXP1.0 version, there is no direct class and method to process the write problem of the XML document, and you need to use some of the other auxiliary classes. In the JAXP1.1 version, support for XSLT is introduced, so-called XSLT is a new document structure after translation, which is translation to the XML document. With this newly added function, we can easily return newly generated or modified DOM trees to the XML file. Let's take a look at the implementation of the code. This code is the main function of this code is to Links. Add a new link node in the .xml file: import javax.xml.parsers. *; import javax.xml.Transform. *; import javax.xml.transform.dom.domsource; import javax.xml.transform.Stream.StreamResult Import org.w3c.dom. *;

Several classes in the newly introduced java.xml.Transform package are used to process XSLT transformations. We want to join a new Link node in the XML file above, but you still have to read into the links.xml file, build a DOM tree, then modify this DOM tree (add node), and finally change the modified DOM Write back to the links.xml file:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder (); Document doc = builder.parse ( "links.xml"); doc.normalize (); // --- obtain variable ---- String Text = "hanzhong's homepage"; string url = "www.hzliu.com"; string author = "hzliu liu"; string discrtion = "a site from hanzhong liu, give u lots of suprise !!!";

In order to see the key, simplify the program, we use the content you want to join to the memory String object, and actual operations often use an interface to extract the user input, or extract the desired content from the database through JDBC.

Text textseg; element link = doc.createElement ("link");

It should be clear that no matter what type of Node, Text type is good, Attr type is also good, Element type is good, and their creation is created through the createxxxx () method in the Document object (XXX representative " Specific types to create), so we have to add a link item to the XML document, first create a LINK object:

Element linktext = doc.createElement ("text"); textSeg = doc.createtextNode (text); linktext.Appendchild (textseg); link.appendchild (linktext); ...... The process of creating nodes may be a thousand people, but you need to pay attention Yes, for the text contained in ELEMENT (in the DOM, these text also represents a Node, so you must create the corresponding node, you can't use the element object's setNodeValue () method to set the contents of these Text And you need to set the text with the setNodeValue () method of the created Text object so that you can add the created Element and its text content to the DOM tree. Take a look at the previous code, you will better understand this: doc.getdocumentelement (). Appendchild (link); Finally, don't forget to add a created node to the DOM tree. The Document class GetDocumentElement () method returns the Element object that represents the root node of the document. In the XML document, the root node must be unique.

TransformerFactory tFactory = TransformerFactory.newInstance (); Transformer transformer = tFactory.newTransformer (); DOMSource source = new DOMSource (doc); StreamResult result = new StreamResult (new Java.io.File ( "links.xml")); transformer. Transform (Source, Result);

Then use XSLT to output the DOM tree. The TransformerFactory here also applies the factory model to make the specific code independent of the specific transducer. The implementation method is the same as DocumentBuilderFactory, and it will not be described here. Transformer class TRANSFROM method accepts two parameters, a data source Source and an output target Result. The DOMSource and StreamResult are used here, which can output the contents of the DOM to an output stream. When this output stream is a file, the content of the DOM is written to the file.

转载请注明原文地址:https://www.9cbs.com/read-129453.html

New Post(0)