JAva解析xmlでよく使われる3つの方法
19482 ワード
(I)Java DOMによるXMLの解析
1> DOM
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilderFactory;
2> DOM DOM
DocumentBuilder dombuilder=domfac.newDocumentBuilder();
javax.xml.parsers.DocumentBuilderFactory newDocumentBuilder() DOM
3> XML , DOM
InputStream is=new FileInputStream("bin/library.xml");
InputStream 。
4> XML , Document
Document doc=dombuilder.parse(is);
XML org.w3c.dom.Document , Document
5> XML
Element root=doc.getDocumentElement();
DOM org.w3c.dom.Element 。
6>
NodeList books=root.getChildNodes();
for(int i=0;i<books.getLength();i++){
Node book=books.item(i);
}
org.w3c.dom.NodeList , ,
7>
String email=book.getAttributes().getNamedItem("email").getNodeValue();
System.out.println(email);
, 。 Node.ELEMENT_NODE
8>
for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()){
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getNodeName().equals("name")){
String name=node.getNodeValue();
String name1=node.getFirstChild().getNodeValue();
...
if(node.getNodeName().equals("price")){
String price=node.getFirstChild().getNodeValue();
...
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<conn-params>
<conn-url>jdbc:mysql://192.168.101.7:3306/bbs</conn-url>
<conn-driver>com.mysql.jdbc.Driver</conn-driver>
<conn-username>root</conn-username>
<conn-password>root</conn-password>
</conn-params>
<person>
<user>
<username>xzc</username>
<password>sdf23223</password>
<birthday>2012-01-23</birthday>
</user>
<user>
<username> </username>
<password>wuchuang3223</password>
<birthday>2002-01-03</birthday>
</user>
</person>
</xml>
////////////////////////////////////////////////////////////////////////////////////
package com.xcz.xml;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import com.xcz.po.User;
import com.xcz.xml.util.DomUtil;
public class Dom4Xml {
private DomUtil domUtil = new DomUtil();
public static void main(String[] args) {
try {
File f = new File("src/jdbc-params.xml");
List<User> list = new Dom4Xml().domUtil.parseXml(f);
for (User user : list) {
System.out.println(user);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
////////////////////////////////////////////////////////////////////////////////////
package com.xcz.xml.util;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.xcz.po.User;
public class DomUtil {
private DocumentBuilderFactory factory;
private DocumentBuilder builder;
private Document document;
private List<User> list;
/**
* DocumentBuilderFactory
* @return DocumentBuilderFactory
*/
private DocumentBuilderFactory getDocumentBuilderFactory(){
return DocumentBuilderFactory.newInstance();
}
/**
* DocumentBulider
* @param DocumentBuilderFactory fac
* @return DocumentBuilder
* @throws ParserConfigurationException
*/
private DocumentBuilder getDocumentBuilder(DocumentBuilderFactory fac) throws ParserConfigurationException{
return fac.newDocumentBuilder();
}
/**
* : XML
* @param file xml
* @return List<User>
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
public List<User> parseXml(File file) throws ParserConfigurationException, SAXException, IOException{
factory = getDocumentBuilderFactory();
builder = getDocumentBuilder(factory);
document = builder.parse(file);
List<User> list = parseXmlProcess(document);
return list;
}
/**
* :xml
* @param document
* @return List<User>
*/
private List<User> parseXmlProcess(Document document){
list = new ArrayList<User>();
//
Element root = document.getDocumentElement();
//
NodeList childNodes = root.getChildNodes();
for(int i=0; i<childNodes.getLength(); i++){// :getLength()
Node node = childNodes.item(i);// index :item(index)
if("person".equals(node.getNodeName())){// tagName:getNodeName()
NodeList pChildNodes = node.getChildNodes();
for(int p=0; p<pChildNodes.getLength(); p++){
Node pNode = pChildNodes.item(p);
if("user".equals(pNode.getNodeName())){
User user = new User();
NodeList uChildNodes = pNode.getChildNodes();
for(int u=0;u<uChildNodes.getLength();u++){
Node uNode = uChildNodes.item(u);
if("username".equals(uNode.getNodeName())){
user.setUsername(uNode.getTextContent());// :getTextContent()
}
if("password".equals(uNode.getNodeName())){
user.setPassword(uNode.getTextContent());
}
if("birthday".equals(uNode.getNodeName())){
user.setBirthday(uNode.getTextContent());
}
}
list.add(user);
}
}
}
}
return list;
}
}
(II)Java SAX XML
<person>
<user>
<username> </username>
<password>6626310xie</password>
<sex> </sex>
<birthday>1988/11/28</birthday>
<headpic>
<pictitle>ermao</pictitle>
<picurl>images/head1.jpg</picurl>
</headpic>
</user>
</person>
xml , User 。
////////////////////////////////////////////////////////////////////////////////////
package com.xcz.xml;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import com.xcz.util.SaxUtil;
public class Sax4XML {
public static void main(String[] args) {
try {
//1. factory
SAXParserFactory factory = SAXParserFactory.newInstance();
//2. parser
SAXParser parser = factory.newSAXParser();
//3.
SaxUtil su = new SaxUtil();
//4.
parser.parse(new File("src/user-params.xml"), su);
System.out.println(su.getUser());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
////////////////////////////////////////////////////////////////////////////////////
package com.xcz.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.xcz.po.HeadPic;
import com.xcz.po.User;
/**
* xml
*
* , :ContentHandler,DTDHandler, EntityResolver ErrorHandler
* :DefaultHandler
*/
public class SaxUtil extends DefaultHandler {
private User user;
private HeadPic headPic;
private String content;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
content = new String(ch, start, length);
}
//
@Override
public void startDocument() throws SAXException {
super.startDocument();
}
//
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
//
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException
{
if("user".equals(name))
{
user = new User();
}
if("headpic".equals(name))
{
headPic = new HeadPic();
}
}
//
@Override
public void endElement(String uri, String localName, String name)
throws SAXException
{
if("username".equals(name))
{
user.setUsername(content);
}
if("password".equals(name))
{
user.setPassword(content);
}
if("sex".equals(name))
{
user.setSex(content);
}
if("birthday".equals(name))
{
try {
user.setBirthday(sdf.parse(content));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if("pictitle".equals(name))
{
headPic.setPicTitle(content);
}
if("picurl".equals(name))
{
headPic.setPicUrl(content);
user.setHeadPic(headPic);
}
}
public User getUser(){
return user;
}
}
[ ]
(1).
SAX DOM , SAX メモリ DOM 。DOM , xml , DOM 。 ,SAX , XML (XML ) XML XML 。
(2)
SAX , DOM 。
[ ]
SAX XML , 。
XML 。 , DTD IDREF DTD ID 。 SAX , ID IDREF , 。 , IDREF ID, , , 。