xml这样的文件格式在非常多时候都是非常适合我们用来存取数据的,所以利用程序来生成xml文件和解析xml文件就显得比較重要了。在dom中是把每个元素都看做是一个节点Node的,全部页面上的属性、元素等都是继承自Node的,所以当获取到的是一个Node。而你知道它实际的类型并须要使用的时候就可能会须要进行类型转换了。
Element root = document.getDocumentElement();//获取根节点
以下是代码:
生成xml:
- public void geneXmlByDom() throws Exception {
- //step1:获得一个DocumentBuilderFactory
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- //step2:获得一个DocumentBuilder
- DocumentBuilder db = factory.newDocumentBuilder();
- //step3:新建一个Document对象
- Document document = db.newDocument();
- //step4:创建一个根节点
- Element rootElement = document.createElement("Persons");
- ;i<5;i++) {
- //step5:创建一个节点
- Element person = document.createElement("person");
- //step6:为该节点设定属性
- person.setAttribute("id", "id_"+i);
- Element name = document.createElement("name");
- //为节点设定文本内容
- name.setTextContent("name_"+i);
- Element address = document.createElement("address");
- address.setTextContent("address_"+i);
- Element email = document.createElement("email");
- email.setTextContent("email_"+i);
- person.appendChild(name);
- person.appendChild(address);
- person.appendChild(email);
- //step7:为某一元素节点设立子节点
- rootElement.appendChild(person);
- }
- //step8:把刚刚建立的根节点加入到document对象中
- document.appendChild(rootElement);
- //step9:获得一个TransformerFactory对象
- TransformerFactory transformerFactory = TransformerFactory.newInstance();
- //step10:获得一个Transformer对象
- Transformer transformer = transformerFactory.newTransformer();
- //step11:把document对象用一个DOMSource对象包装起来
- Source xmlSource = new DOMSource(document);
- //step12:建立一个存储目标对象
- Result outputTarget = new StreamResult(new File("persons.xml"));
- //step13:生成对应的xml文件
- transformer.transform(xmlSource, outputTarget);
- }
生成的xml文件是这个样子
- <?
xml version="1.0" encoding="UTF-8" standalone="no"?
>
- <Persons>
- <person id="id_0">
- <name>name_0</name>
- <address>address_0</address>
- <email>email_0</email>
- </person>
- <person id="id_1">
- <name>name_1</name>
- <address>address_1</address>
- <email>email_1</email>
- </person>
- <person id="id_2">
- <name>name_2</name>
- <address>address_2</address>
- <email>email_2</email>
- </person>
- <person id="id_3">
- <name>name_3</name>
- <address>address_3</address>
- <email>email_3</email>
- </person>
- <person id="id_4">
- <name>name_4</name>
- <address>address_4</address>
- <email>email_4</email>
- </person>
- </Persons>
在进行输出写到文件的时候能够设置一些Transformer输出的參数,如xml的编码。或者输出形式等。
- transformer.setOutputProperty("encoding", "UTF-8");//设定文档编码。属性也能够使用OutputKeys的静态常量属性设定
- transformer.setOutputProperty(OutputKeys.METHOD, "xml");//输出方式,能够是xml、html和text
以下的解析也是基于这个文件的。
解析xml:
- public void parseXmlByDom() throws Exception {
- //step1:获得DocumentBuilderFactory
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- //step2:获得DocumentBuilder
- DocumentBuilder db = factory.newDocumentBuilder();
- //step3:把须要解析的xml文件载入到一个document对象中
- Document document = db.parse("persons.xml");
- //获取全部名称为person的元素
- NodeList nodeList = document.getElementsByTagName("person");
- ;i<nodeList.getLength();i++) {
- //由于这里我知道它就是一个Element对象,所以进行了强转
- Element person = (Element)nodeList.item(i);
- //获得Element对象的属性
- String id = person.getAttribute("id");
- //由于这里我知道仅仅有一个对象,所以就直接item(0)了,
- //由于Dom是把每一个对象都看做是一个节点的,所以假设在item(0)的时候直接取getNodeValue()是取不到值的,
- //仅仅有是TextNode的时候用那个方法才干够取到值,假设想直接取值就用getTextContent()
- ).getTextContent();
- ).getTextContent();
- ).getTextContent();
- System.out.println(" id = "+id+" \r\n name = "+name+" \r\n address = "+address+" \r\n email = "+email);
- System.out.println("-------------------------------------");
- }
- }
解析后输出的结果:
- id = id_0
- name = name_0
- address = address_0
- email = email_0
- -------------------------------------
- id = id_1
- name = name_1
- address = address_1
- email = email_1
- -------------------------------------
- id = id_2
- name = name_2
- address = address_2
- email = email_2
- -------------------------------------
- id = id_3
- name = name_3
- address = address_3
- email = email_3
- -------------------------------------
- id = id_4
- name = name_4
- address = address_4
- email = email_4
- -------------------------------------