天天看點

java中使用xml

//DOM方式

package com.neusoft.xml;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

public class DomXmlSample {

 public static void main(String[] args) throws ParserConfigurationException,

   SAXException, IOException {

  // TODO Auto-generated method stub

  // 1.獲得一個新DocumentBuilderFactory執行個體(定義工廠API,使應用程式能夠從 XML 文檔擷取生成 DOM

  // 對象樹的解析器)

  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

  // 2.使用DocumentBuilderFactory建構DocumentBuilder(定義 API, 使其從 XML 文檔擷取 DOM

  // 文檔執行個體。使用此類,應用程式員可以從 XML 擷取一個 Document)

  DocumentBuilder db = dbf.newDocumentBuilder();

  // 3.使用DocumentBuilder的parse()方法解析檔案

  // 4.将已解析的文檔存儲在Document對象中

  Document document = db.parse("testDocument.xml");

  // 5.使用getElementsByTagName()方法獲得元素

  NodeList bookList = document.getElementsByTagName("book");

  // 擷取所有的子節點

  for (int i = 0; i < bookList.getLength(); i++) {

   System.out.println("========第" + i + "記錄============");

   Node node = bookList.item(i);

   NodeList childNodes = node.getChildNodes();

   for (int j = 0; j < childNodes.getLength(); j++) {

    Node childN = childNodes.item(j);

    if (childN.getFirstChild() != null) {

     System.out.print(childN.getNodeName());

     System.out.println(":"

       + childN.getFirstChild().getNodeValue());

    }

   }

  }

 }

}

package com.neusoft.xml;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.List;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.JDOMException;

import org.jdom.input.SAXBuilder;

import org.jdom.output.XMLOutputter;

public class JdomXmlSample {

 public static void main(String[] args) throws JDOMException, IOException {

  // TODO Auto-generated method stub

SAXBuilder builder=new SAXBuilder(false);//表示使用預設的解析器

Document document=builder.build("testDocument.xml");

Element root=document.getRootElement();

List books=root.getChildren("book");

for(int i=0;i<books.size();i++)

{

 Element book=(Element)books.get(i);

 Element code=book.getChild("code");

 Element title=book.getChild("title");

 Element author=book.getChild("author");

 Element price=book.getChild("price");

 System.out.println("code="+code.getText()+"\t"+"title="+title.getText()

                +"\tauthor="+author.getText()+"\t"+"price="+price.getText());

// System.out.println("code:"+book.getChildText("code")+

//              "\ttitle:"+book.getChildText("title")

//              +"\tauthor:"+book.getChildText("author")+

//              "\tprice:"+book.getChildText("price"));

 book.getChild("price").setText("100");

 }

XMLOutputter  outputter=new XMLOutputter();

outputter.output(document, new FileOutputStream("abc.xml"));

 }

}

a.xml

<?xml version="1.0" encoding="gb2312"?>

<!DOCTYPE abc [

<!ELEMENT note (男|女)>

]>

<abc>

<note>男</note>

</abc>

abc.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE booklist>

<booklist>

 <book>

  <code>1</code>

  <title>XML講解</title>

  <author>張三</author>

  <price>100</price>

 </book>

 <book>

  <code>2</code>

  <title>HTML講解</title>

  <author>李四</author>

  <price>100</price>

 </book>

 <book>

  <code>3</code>

  <title>JSP講解</title>

  <author>王五</author>

  <price>100</price>

 </book>

</booklist>

testDocument.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE booklist>

<booklist>

 <book>

  <code>1</code>

  <title>XML講解</title>

  <author>張三</author>

  <price>100</price>

 </book>

 <book>

  <code>2</code>

  <title>HTML講解</title>

  <author>李四</author>

  <price>100</price>

 </book>

 <book>

  <code>3</code>

  <title>JSP講解</title>

  <author>王五</author>

  <price>100</price>

 </book>

</booklist>

xmltest.dtd

<?xml version="1.0" encoding="UTF-8"?>

<!ELEMENT booklist (book)+>

<!ELEMENT book (code,title,author,price)>

<!ELEMENT code (#PCDATA)>

<!ELEMENT title (#PCDATA)>

<!ELEMENT author (#PCDATA)>

<!ELEMENT price  (#PCDATA)>

<!ATTLIST book id ID #REQUIRED

               type (IT|科普) "科普">