写了一篇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>