生成XML 解析
Posted 猪八戒1.0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成XML 解析相关的知识,希望对你有一定的参考价值。
1.
2.
3.
4.使用Java默认的dom生成方式
Student类
package test;
public class Student
private int stuid;
private String stuname;
private int stuage;
private String stuaddress;
public int getStuid()
return stuid;
public void setStuid(int stuid)
this.stuid = stuid;
public String getStuname()
return stuname;
public void setStuname(String stuname)
this.stuname = stuname;
public int getStuage()
return stuage;
public void setStuage(int stuage)
this.stuage = stuage;
public String getStuaddress()
return stuaddress;
public void setStuaddress(String stuaddress)
this.stuaddress = stuaddress;
XMLHelper类
package test;
import java.io.File;
import java.util.List;
import javax.lang.model.element.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/**
* @author 猪八戒
* Java默认的dom方式(jdk提供的Java类)
*
*/
public class XMLHelper
public static void createXML1(List<Student> studentList) throws Exception
//得到DOM解析器工厂
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//创建DocumentBuilder对象
DocumentBuilder documentBuilder=factory.newDocumentBuilder();
//创建Document对象
org.w3c.dom.Document document=documentBuilder.newDocument();
//设置文档对象的文件头
document.setXmlStandalone(true);
//创建根元素
org.w3c.dom.Element rooElement=document.createElement("studentlist");
//遍历集合
for(Student student:studentList)
//创建子元素
org.w3c.dom.Element studentElement= document.createElement("student");
//为子元素设置属性
studentElement.setAttribute("stuid", String.valueOf(student.getStuid()));
//创建子子元素
org.w3c.dom.Element stunameElement=document.createElement("stuname");
//为子子元素设置具体参数值
stunameElement.setTextContent(student.getStuname());
//创建子子元素
org.w3c.dom.Element stuageElement=document.createElement("stuage");
//为子子元素设置具体参数值
stuageElement.setTextContent(String.valueOf(student.getStuage()));
//创建子子元素
org.w3c.dom.Element stuaddressElement=document.createElement("stuaddress");
//为子子元素设置具体参数值
stuaddressElement.setTextContent(student.getStuaddress());
//将子子元素添加到子元素中
studentElement.appendChild(stunameElement);
studentElement.appendChild(stuageElement);
studentElement.appendChild(stuaddressElement);
//将子元素添加到根元素中
rooElement.appendChild(studentElement);
//将根元素添加到Document对象中
document.appendChild(rooElement);
//将document对象写出到文件中
File file=new File("src//test//student.xml");
//创建TransformerFactory
TransformerFactory tff= TransformerFactory.newInstance();
//创建Transformer对象
Transformer tf=tff.newTransformer();
//输出内容是否使用换行
tf.setOutputProperty(OutputKeys.INDENT, "yes");
//创建xml文件并写入内容
tf.transform(new DOMSource(document), new StreamResult(file));
Main类
package test;
import java.util.ArrayList;
import java.util.List;
public class Main
public static void main(String[] args) throws Exception
Student student1=new Student();
student1.setStuid(1001);
student1.setStuname("zhangsan");
student1.setStuage(23);
student1.setStuaddress("西安");
Student student2=new Student();
student2.setStuid(1002);
student2.setStuname("lisi");
student2.setStuage(24);
student2.setStuaddress("北京");
Student student3=new Student();
student3.setStuid(1003);
student3.setStuname("wangwu");
student3.setStuage(24);
student3.setStuaddress("湖北");
List<Student> studentList=new ArrayList<>();
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
XMLHelper.createXML1(studentList);
5.下载jdom,dom4j,SAX
DOM:在现在的Java JDK里都自带了,在xml-apis.jar包里
SAX:http://sourceforge.net/projects/sax/
jdom:http://jdom.org/downloads/index.html
dom4j:http://sourceforge.net/projects/dom4j/
6.使用第三方包dom4j
package test;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
/**
* @author 猪八戒
* 使用第三方包dom4j
*
*/
public class XMLHelper
public static void createXML2(List<Student> studentList)throws Exception
//创建Document对象
Document document= DocumentHelper.createDocument();
//创建根元素
Element rootElement = document.addElement("studentlist");
for (Student student:studentList)
Element studentElement = rootElement.addElement("student");
studentElement.addAttribute("stuid",String.valueOf(student.getStuid()));
//也可先给名字再用setText赋值
Element stunametElement =studentElement.addElement("stuname",student.getStuname());
Element stunagetElement =studentElement.addElement("stuage",String.valueOf(student.getStuage()) );
Element stuaddressElement =studentElement.addElement("stuaddress",student.getStuaddress());
//设置生成xml的格式
OutputFormat format = OutputFormat.createPrettyPrint();
//设置编码格式
format.setEncoding("UTF-8");
//创建XML字符输出流
XMLWriter writer = new XMLWriter(new FileOutputStream(new File("src//test//student2.xml")), format);
//设置是否转义,默认使用转义字符
writer.setEscapeText(false);
//写出Document对象
writer.write(document);
//关闭流
writer.close();
7.使用第三方包jdom
package test;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
/**
* @author 猪八戒
* 使用第三方包jdom
*
*/
public class XMLHelper
public static void createXML3(List<Student> studentList) throws Exception
Element rootElement=new Element("studentlist") ;
for(Student student:studentList)
Element studentElement = new Element("student");
studentElement.setAttribute("stuid",String.valueOf(student.getStuid()));
Element stunameElement = new Element("stuname");
stunameElement.setText(student.getStuname());
Element stuageElement = new Element("stuage");
stuageElement.setText(String.valueOf(student.getStuage()));
Element stuaddressElement = new Element("stuaddress");
stuaddressElement.setText(student.getStuaddress());
studentElement.addContent(stunameElement);
studentElement.addContent(stuageElement);
studentElement.addContent(stuaddressElement);
rootElement.addContent(studentElement);
Document document=new Document(rootElement);
//输出Document对象
Format format= Format.getCompactFormat();
//设置换行Tab或空格
format.setIndent(" ");
format.setEncoding("UTF-8");
//创建XMLOutputter的对象
XMLOutputter outputer=new XMLOutputter(format);
//写出Document
outputer.output(document,new FileOutputStream(new File("src//test//student3.xml")));
以上是关于生成XML 解析的主要内容,如果未能解决你的问题,请参考以下文章