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"); for (int i=0;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"); for (int i=0;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() String name = person.getElementsByTagName("name").item(0).getTextContent(); String address = person.getElementsByTagName("address").item(0).getTextContent(); String email = person.getElementsByTagName("email").item(0).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 -------------------------------------