轉 :http://blog.csdn.net/zlp5201/article/details/7163683
寫了一篇xml的文章,點發布發表不上,點舍去就他媽的一下子全沒了,連回收站和草稿箱都沒有了,真不知道怎么想CSDN
參考http://blog.csdn.net/yangzl2008/article/details/7045369
廢話不多說了,貼代碼:
- /**
- *
- */
- package com.zlp.test.xml;
- import java.io.File;
- import java.util.Iterator;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- import org.dom4j.DocumentException;
- import org.dom4j.Element;
- import org.dom4j.io.SAXReader;
- import org.w3c.dom.Document;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.Attributes;
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- /**
- * @author Administrator
- *
- */
- public class XmlRead extends DefaultHandler{
- /**
- * @param args
- */
- /*
- * DOM
- */
- public void TestDOM(){
- DocumentBuilderFactory factory = DocumentBuilderFactory
- .newInstance();
- Document doc = null;
- try {
- DocumentBuilder builder = factory.newDocumentBuilder();
- doc = builder.parse(new File("test.xml"));
- } catch (Exception e) {
- e.printStackTrace();
- }
- String str = doc.getElementsByTagName("name").item(0).getFirstChild().getNodeValue().trim();
- System.out.println(str);
- }
- /*
- * dom4j
- */
- public void Dom4jReadTest(){
- File f = new File("test.xml");
- SAXReader saxReader = new SAXReader();
- org.dom4j.Document document = null;
- try {
- document = saxReader.read(f);
- } catch (DocumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Element rootElement = document.getRootElement();
- Iterator iterator = rootElement.elementIterator("node");
- for (; iterator.hasNext();) {
- Element other = (Element)iterator.next();
- System.out.println(other.elementTextTrim("name"));
- }
- }
- public void SAX(){
- java.util.Stack tags = new java.util.Stack();
- SAXParserFactory saxparserfactory = SAXParserFactory.newInstance();
- try {
- SAXParser parser = saxparserfactory.newSAXParser();
- parser.parse(new InputSource("test.xml"), new XmlRead());
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- java.util.Stack tags = new java.util.Stack();
- public void startElement(String uri, String localName, String qName,Attributes attrs) {
- tags.push(qName);
- }
- public void characters(char ch[], int start, int length)
- throws SAXException {
- String tag = (String) tags.peek();
- if (tag.equals("name")) {
- System.out.print("name" + new String(ch, start, length));
- }
- if (tag.equals("space")) {
- System.out.println("space:" + new String(ch, start, length));
- }
- }
- public static void main(String[] args) {
- //new XmlRead().TestDOM();
- new XmlRead().Dom4jReadTest();
- //new XmlRead().SAX();
- }
- }
test.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <list>
- <node>
- <name>
- weidewei
- </name>
- <space>
- http://wishlife.iteye.com
- </space>
- </node>
- </list>
Sax解析xml
- package com.zlp.test.xml;
- import java.io.File;
- import java.util.Vector;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- public class PraseXML extends DefaultHandler
- {
- private Vector<String> tagName;
- private Vector<String> tagValue;
- private int step;
- // 開始解析XML文件
- public void startDocument() throws SAXException
- {
- tagName = new Vector<String>();
- tagValue = new Vector<String>();
- step = 0;
- }
- // 結束解析XML文件
- public void endDocument() throws SAXException
- {
- for (int i = 0; i < tagName.size(); i++)
- {
- if (!tagName.get(i).equals("") || tagName.get(i) != null)
- {
- System.out.println("節點名稱:" + tagName.get(i));
- System.out.println("節點值:" + tagValue.get(i));
- }
- }
- }
- /**
- * 在解釋到一個開始元素時會調用此方法.但是當元素有重復時可以自己寫算法來區分
- * 這些重復的元素.qName是什么? <name:page ll=""></name:page>這樣寫就會拋出SAXException錯誤
- * 通常情況下qName等於localName
- */
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException
- {
- // 節點名稱
- tagName.add(qName);
- // 循環輸出屬性
- for (int i = 0; i < attributes.getLength(); i++)
- {
- // 獲取屬性名稱
- System.out.println("屬性名稱:" + attributes.getQName(i));
- // 獲取屬性值
- System.out.println("屬性值:"
- + attributes.getValue(attributes.getQName(i)));
- }
- }
- /**
- * 在遇到結束標簽時調用此方法
- */
- public void endElement(String uri, String localName, String qName)
- throws SAXException
- {
- step = step + 1;
- }
- /**
- * 讀取標簽里的值,ch用來存放某行的xml的字符數據,包括標簽,初始大小是2048,
- * 每解釋到新的字符會把它添加到char[]里。 * 注意,這個char字符會自己管理存儲的字符,
- * 並不是每一行就會刷新一次char,start,length是由xml的元素數據確定的,
- * 暫時找不到規律,以后看源代碼.
- *
- * 這里一個正標簽,反標簽都會被執行一次characters,所以在反標簽時不用獲得其中的值
- */
- public void characters(char ch[], int start, int length)
- throws SAXException
- {
- // 只要當前的標簽組的長度一至,值就不賦,則反標簽不被計划在內
- if (tagName.size() - 1 == tagValue.size())
- {
- tagValue.add(new String(ch, start, length));
- }
- }
- public static void main(String[] args)
- {
- String filename = "test.xml";
- SAXParserFactory spf = SAXParserFactory.newInstance();
- try
- {
- SAXParser saxParser = spf.newSAXParser();
- saxParser.parse(new File(filename), new PraseXML());
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public Vector getTagName()
- {
- return tagName;
- }
- public void setTagName(Vector tagName)
- {
- this.tagName = tagName;
- }
- public Vector getTagValue()
- {
- return tagValue;
- }
- public void setTagValue(Vector tagValue)
- {
- this.tagValue = tagValue;
- }
- }
在貼上一個simplejee中讀取xml的文件。
- package com.yuqiaotech.simplejee.xml;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStreamWriter;
- import java.io.Writer;
- import java.util.Iterator;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- import javax.xml.transform.OutputKeys;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerConfigurationException;
- import javax.xml.transform.TransformerException;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import javax.xml.transform.stream.StreamSource;
- import org.dom4j.DocumentException;
- import org.dom4j.io.OutputFormat;
- import org.dom4j.io.SAXReader;
- import org.dom4j.io.XMLWriter;
- import org.w3c.dom.Document;
- import org.w3c.dom.NamedNodeMap;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- import com.sun.org.apache.xpath.internal.XPathAPI;
- /**
- * 大體代碼是從http://www.javaeye.com/topic/181865抄來的。
- * 下面這個是被廣泛抄襲的,關於java里讀取xml的概要介紹。
- * http://blog.csdn.net/geekwang/archive/2008/05/25/2480504.aspx
- *
- * 我主要是把從絕對路徑讀取xml換成了從classpath讀取。
- * 另外添加了Transformer和xslt,以及XPath的演示,以及相關的一些鏈接。
- *
- * 另外可以搜一下jaxp了解這個規范的相關內容。
- *
- * @author YUQIAOTECH
- *
- */
- public class SimpleSample {
- static String xmlName = "test.xml";
- static String xlst = "xslt.xsl";
- static String dom4jSaveTo = "c:/text.xml";
- static String xsltSaveTo = "c:/text2.html";
- /**
- * DOM方式
- */
- public void DOM() {
- long lasting = System.currentTimeMillis();
- try {
- InputStream in = SimpleSample.class.getResourceAsStream(xmlName);
- DocumentBuilderFactory factory = DocumentBuilderFactory
- .newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document doc = builder.parse(in); //注意這里的Document是org.w3c.dom包下的
- NodeList nl = doc.getElementsByTagName("node");
- for (int i = 0; i < nl.getLength(); i++) {
- System.out.println("|| Name: |"
- + doc.getElementsByTagName("name").item(i)
- .getFirstChild().getNodeValue());
- System.out.println("||Space: |"
- + doc.getElementsByTagName("space").item(i)
- .getFirstChild().getNodeValue());
- System.out.println("-------------------------------------------------"); }
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("耗時:"
- + (System.currentTimeMillis() - lasting) + " MS");
- }
- class SaxHandler extends DefaultHandler{
- java.util.Stack tags = new java.util.Stack();
- public void startElement(String uri, String localName, String qName,
- Attributes attrs) {
- tags.push(qName);
- }
- public void characters(char ch[], int start, int length)
- throws SAXException {
- String tag = (String) tags.peek();
- if (tag.equals("name")) {
- System.out.println("|| Name: |" + new String(ch, start, length));
- }
- if (tag.equals("space")) {
- System.out.println("||Space: |" + new String(ch, start, length));
- }
- System.out.println("-------------------------------------------------");
- }
- }
- /**
- * SAX方式
- */
- public void SAX() {
- long lasting = System.currentTimeMillis();
- try {
- InputStream in = SimpleSample.class.getResourceAsStream(xmlName);
- SAXParserFactory sf = SAXParserFactory.newInstance();
- SAXParser sp = sf.newSAXParser();
- SaxHandler reader = new SaxHandler();
- sp.parse(in, reader);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("SAX 耗時:"
- + (System.currentTimeMillis() - lasting) + " MS");
- }
- /**
- * 我懶得去了解JDOM了 :-)。
- * JDOM方式
- */
- // public void JDOM() {
- // long lasting = System.currentTimeMillis();
- // try {
- // SAXBuilder builder = new SAXBuilder();
- // org.jdom.Document doc = builder.build(new File("F:/xmltest.xml"));
- // Element foo = doc.getRootElement();
- // List allChildren = foo.getChildren();
- // for (int i = 0; i < allChildren.size(); i++) {
- // System.out.println("|| Name: |"
- // + ((Element) allChildren.get(i)).getChild("name")
- // .getText());
- // System.out.println("||Space: |"
- // + ((Element) allChildren.get(i)).getChild("space")
- // .getText());
- // System.out.println("-------------------------------------------------"); }
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- // System.out.println("JDOM RUNTIME:"
- // + (System.currentTimeMillis() - lasting) + " MS");
- // }
- /**
- * DOM4J方式
- */
- public void DOM4J() {
- long lasting = System.currentTimeMillis();
- try {
- InputStream in = SimpleSample.class.getResourceAsStream(xmlName);
- SAXReader reader = new SAXReader();
- org.dom4j.Document doc = reader.read(in); //注意這里的Document是org.dom4j包下的
- org.dom4j.Element root = doc.getRootElement();
- org.dom4j.Element foo;
- for (Iterator i = root.elementIterator("node"); i.hasNext();) {
- foo = (org.dom4j.Element) i.next();
- System.out.println("|| Name: |" + foo.elementText("name"));
- System.out.println("||Space: |" + foo.elementText("space"));
- System.out.println("-------------------------------------------------");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("DOM4J 耗時:"
- + (System.currentTimeMillis() - lasting) + " MS");
- }
- /**
- * 調用dom4j的保存方法。
- *
- * @throws DocumentException
- * @throws IOException
- */
- public static void saveDocByDom4J() throws DocumentException, IOException{
- Writer out = new OutputStreamWriter(new FileOutputStream(dom4jSaveTo ),"GBK");
- OutputFormat format = OutputFormat.createPrettyPrint();
- XMLWriter writer = new XMLWriter( out, format );
- InputStream in = SimpleSample.class.getResourceAsStream(xmlName);
- SAXReader reader = new SAXReader();
- org.dom4j.Document doc = reader.read(in);
- writer.write( doc );
- out.close();
- }
- /**
- * 使用Transformer和xslt。
- * http://www.ibm.com/developerworks/cn/xml/x-xslt/
- *
- * @throws ParserConfigurationException
- * @throws SAXException
- * @throws IOException
- */
- public static void saveByTransformer() throws ParserConfigurationException, SAXException, IOException {
- try {
- InputStream in = SimpleSample.class.getResourceAsStream(xmlName);
- InputStream inXsl = SimpleSample.class.getResourceAsStream(xlst);
- DocumentBuilderFactory factory = DocumentBuilderFactory
- .newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document doc = builder.parse(in);
- StreamSource style = new StreamSource(inXsl);
- TransformerFactory tFactory = TransformerFactory.newInstance();
- Transformer transformer = tFactory.newTransformer(style);
- transformer.setOutputProperty(OutputKeys.ENCODING, "GBK");
- DOMSource source = new DOMSource(doc);
- StreamResult result = new StreamResult(new File(xsltSaveTo));
- transformer.transform(source, result);
- } catch (TransformerConfigurationException e) {
- throw new RuntimeException(e.getMessage(), e);
- } catch (TransformerException e) {
- throw new RuntimeException(e.getMessage(), e);
- }
- }
- //**********************XPath*****************************
- /**
- * 返回指定的節點。
- *
- * @param topNode
- * @param xPath
- * @return
- */
- public static Node selectSingleNode(Node topNode, String xPath) {
- try {
- return XPathAPI.selectSingleNode(topNode, xPath);
- } catch (TransformerException e) {
- System.out.println(e.getMessage() + " xPath=" + xPath);
- throw new RuntimeException(e.getMessage(), e);
- }
- }
- /**
- * 根據屬性名獲取屬性節點。
- *
- * @param node
- * @param attributeName
- * @return
- */
- public static Node getAttributeNode(Node node, String attributeName) {
- NamedNodeMap namedNodeMap = node.getAttributes();
- return namedNodeMap.getNamedItem(attributeName);
- }
- /**
- * 幾個方法的組合。
- *
- * @param node
- * @param xPath
- * @param attributeName
- * @return
- */
- public static String getAttributeNodeByXPath(Node node, String xPath,
- String attributeName) {
- Node rtn = null;
- Node selectedNode = selectSingleNode(node, xPath);
- if (selectedNode != null) {
- rtn = getAttributeNode(selectedNode, attributeName);
- }
- if(rtn == null)return null;
- return rtn.getNodeValue();
- }
- /**
- * http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html
- * http://www.ibm.com/developerworks/cn/xml/x-wxxm35.html
- *
- * @throws ParserConfigurationException
- * @throws SAXException
- * @throws IOException
- */
- public static void XPath() throws ParserConfigurationException, SAXException, IOException{
- InputStream in = SimpleSample.class.getResourceAsStream(xmlName);
- DocumentBuilderFactory factory = DocumentBuilderFactory
- .newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document doc = builder.parse(in);
- String attr = getAttributeNodeByXPath(doc,"//node[@id=1]/name","alias");
- System.out.println("alias="+attr);
- }
- public static void main(String arge[]) throws ParserConfigurationException, SAXException, IOException, DocumentException {
- SimpleSample myXML = new SimpleSample();
- System.out.println("=====================DOM=========================");
- myXML.DOM();
- System.out.println("=====================SAX=========================");
- myXML.SAX();
- //System.out.println("=====================JDOM========================");
- //myXML.JDOM();
- System.out.println("=====================DOM4J=======================");
- myXML.DOM4J();
- System.out.println("=====================DOM4J的格式化保存=======================");
- saveDocByDom4J();
- System.out.println("=====================Transformer和xslt的使用=======================");
- saveByTransformer();
- System.out.println("=====================XPath的演示=======================");
- XPath();
- }
- }
test1.xml
- <?xml version="1.0" encoding="gbk"?>
- <list>
- <node id="1"><name alias="李逵">張三</name><space>http://wishlife.javaeye.com</space></node>
- <node><name>李四</name><space>http://user.qzone.qq.com/94611981</space></node>
- </list>
xslt.xsl
- <xsl:transform
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- version="1.0">
- <xsl:variable name="nodes" select="//node"/>
- <xsl:template match="list">
- <html><body>
- <h1>名單</h1>
- <table cellpadding="5">
- <tr>
- <td>姓名</td>
- <td>博客</td>
- </tr>
- <xsl:for-each select="$nodes">
- <tr>
- <td> <xsl:value-of select="./name"/>(<xsl:value-of select="./name/@alias"/>)</td>
- <td> <xsl:value-of select="./space"/></td>
- </tr>
- </xsl:for-each>
- </table>
- </body></html>
- </xsl:template>
- </xsl:transform>
