Home > Backend Development > XML/RSS Tutorial > A brief tutorial on constructing and generating XML in Java

A brief tutorial on constructing and generating XML in Java

高洛峰
Release: 2017-01-11 13:15:51
Original
1669 people have browsed it

This article introduces how to quickly construct an XML fragment when programming in Java, and then output the XML.

XML is often used when developing in daily Java. XML is easy to use, but annoying to write. Is there a simple construction and output method? And look down.

1. Import jar package and namespace

To use XML in Java, it is recommended to import a jar package-dom4j first. This is a jar package specially designed for processing XML, which is very easy to use.

Then import the following three classes:

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
Copy after login

2. Define the XML schema

Before writing anything in the XML fragment, you must first create an XML fragment, or in other words XML Document. In the following program, a Document object is first created, and then a root element (Element) is created on it.

Document document = DocumentHelper.createDocument();
Element root = document.addElement("return");
Copy after login

3. Add child nodes

After you have the root node element, you can add child nodes to it.

Element returnvalue = root.addElement("returnvalue");
Element returninfo = root.addElement("returninfo");
Copy after login

4. Add content to child nodes

You can add content to already created child nodes:

returnvalue.addText("false");
returninfo.addText("get-session-fail");
Copy after login

You can also add content while creating child nodes:

root.addElement("id").addText("12345");
Copy after login

Note that when using addText to add node text content, sometimes we will directly use variables as parameters of the function. If this variable is null, the addText function will report an error. If it is other non-string type, an error will also be reported. You can add an empty string after the parameter to avoid errors.

is as follows:

int id=1;
root.addElement("id").addText(id+"");
Copy after login

5. Output XML

If you just want to get the XML string, then the following sentence will do it.

String output = document.asXML();
Copy after login

If you want to use this XML as the output of the entire web page, then you must:

response.setContentType("text/xml");
response.write(output);
Copy after login

This article only introduces so much about the construction and output of XML in Java. I hope it will be helpful to you. ,Thanks!

For more articles related to the concise tutorial on constructing and generating XML in Java, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template