---
title: "Java XML解析之 JAXP"
author: "softstone"
date: "2008-01-24T14:28:27Z"
tags: []
slug: "java-xml-jaxp"
draft: false
lastmod: "2008-01-24T14:28:27Z"
id: 172899
url: "https://softstone.iteye.com/blog/172899"
---

JAXP严格来说我觉得只能算是一个解析XML的抽象层，Java应用可以通过Jaxp支持多种处理xml的底层实现，在API上沿用了w3c的Dom和Sax接口，乏善可陈。事实上Dom4j通过支持Jaxp也可以拥有Jaxp这层抽象的优点，不明白sun为什么不在Jaxp这层抽象上定义更好用的接口。 

1、使用Sax接口

SAXParserFactory factory = SAXParserFactory.newInstance();  
SAXParser parser = factory.newSAXParser();  
parser.parse(new File(args\[0]), new MyHandler());

//定义Sax回调实现  
class MyHandler extends DefaultHandler {  
    // SAX callback implementations from ContentHandler, ErrorHandler, etc.  
}

2、使用Dom接口

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();     
DocumentBuilder builder = factory.newDocumentBuilder();  
Document doc = builder.parse(in);

//一个遍历的简单示例

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)public void printTree(Node node, String pre)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        switch (node.getNodeType())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        case Node.ATTRIBUTE\_NODE:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            System.out.print(pre + node.getNodeName() + " = ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            System.out.println(node.getNodeValue());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        case Node.DOCUMENT\_NODE:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            Document doc = (Document) node;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            printTree(doc.getDocumentElement(), "");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        case Node.ELEMENT\_NODE:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            System.out.println(pre + node.getNodeName());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            NamedNodeMap map = node.getAttributes();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            for (int i = 0; i &lt; map.getLength(); i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Node attr = map.item(i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                printTree(attr, pre);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            NodeList list = node.getChildNodes();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            for (int i = 0; i &lt; list.getLength(); i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Node child = list.item(i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                printTree(child, pre + "  ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        case Node.TEXT\_NODE:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            if (node.getTextContent() == null  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    || node.getTextContent().trim().equals(""))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            System.out.println(pre + node.getTextContent());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        default:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }

输出Document

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    public void write(Document doc, OutputStream out)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            throws TransformerException, UnsupportedEncodingException,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            IOException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        TransformerFactory tfactory = TransformerFactory.newInstance();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Transformer former = tfactory.newTransformer();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Source source = new DOMSource(doc);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        StringWriter buffer = new StringWriter();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        StreamResult result = new StreamResult(buffer);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        former.transform(source, result);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        out.write(buffer.toString().getBytes("GBK"));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

 上面的DefaultHandler和Document都是w3c定义的，对Java来说都算不上友好。