XML
来源:互联网 发布:手机验钞灯软件2.0 编辑:程序博客网 时间:2024/05/16 17:54
XML SAX: 在读取文档提取相应的标记事件(元素起始、元素结束、文档起始) DOM: 在内存中构造与文档中元素相应的树,可以遍历、搜索、修改 DTD: 验证文档是否正确 JAXP: 用于XML处理的Java API Castor: 开源项目,用于Java对象与XML映射Java代码 1. // 从对象中生成XML 2. private final static String FILENAME = "serial.xml"; 3. public static void main(String[] args) throws IOException { 4. String a = "hard work and best callback"; 5. new SerialDemoXML().write(a); 6. new SerialDemoXML().dump(); 7. } 8. public void write(Object obj) throws IOException { 9. XMLEncoder os = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(FILENAME))); 10. os.writeObject(obj); 11. os.close(); 12. } 13. public void dump() throws IOException { 14. XMLDecoder out = new XMLDecoder(new BufferedInputStream(new FileInputStream(FILENAME))); 15. System.out.println(out.readObject()); 16. out.close(); 17. } 18. serial.xml 格式内容如下: 19. <?xml version="1.0" encoding="UTF-8"?> 20. <java version="1.6.0_02" class="java.beans.XMLDecoder"> 21. <string>hard work and best callback</string> 22. </java> 23. 控 制台输出 24. hard work and best callback 25. 26. // XSLT转换XML 27. XSLT 可以用来对输出格式进行各种控制 28. Transformer tx = TransformerFactory.newInstance().newTransformer(new StreamSource("people.xml")); 29. tx.transform(new StreamSource("people.xml"), new StreamResult("people.html")); 30. 31. // 用SAX解析XML - 主要用于查找关键元素,不用全文遍历 32. public SaxLister() throws SAXException, IOException { 33. XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); 34. parser.setContentHandler(new PeopleHandler()); 35. parser.parse("C:\\StudySource\\javacooksrc2\\xml\\people.xml"); 36. } 37. class PeopleHandler extends DefaultHandler { 38. boolean parent = false; 39. boolean kids = false; 40. public void startElement(String nsURI, String localName, String rawName, Attributes attr) throws SAXException { 41. System.out.println("startElement: " + localName + "," + rawName); 42. if (rawName.equalsIgnoreCase("name")) 43. parent = true; 44. if (rawName.equalsIgnoreCase("children")) 45. kids = true; 46. } 47. public void characters(char[] ch, int start, int length) { 48. if (parent) { 49. System.out.println("Parent: " + new String(ch, start, length)); 50. parent = false; 51. } else if (kids) { 52. System.out.println("Children: " + new String(ch, start, length)); 53. kids = false; 54. } 55. } 56. public PeopleHandler() throws SAXException { 57. super(); 58. } 59. } 60. 61. // DOM解析XML - 遍历整个树 62. String uri = "file:" + new File("C:\\StudySource\\javacooksrc2\\xml\\people.xml").getAbsolutePath(); 63. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 64. DocumentBuilder builder = factory.newDocumentBuilder(); 65. Document doc = builder.parse(uri); 66. NodeList nodes = doc.getChildNodes(); 67. for (int i = 0; i < nodes.getLength(); i++) { 68. Node n = nodes.item(i); 69. switch (n.getNodeType()) { 70. case Node.ELEMENT_NODE: 71. // todo 72. break; 73. case Node.TEXT_NODE: 74. // todo 75. break; 76. } 77. } 78. 79. // 使用DTD或者XSD验证 80. 定 义好DTD或XSD文件 81. XmlDocument doc = XmlDocument.createXmlDocument(uri, true); 82. 83. // 用DOM生成XML 84. DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); 85. DocumentBuilder parser = fact.newDocumentBuilder(); 86. Document doc = parser.newDocument(); 87. Node root = doc.createElement("Poem"); 88. doc.appendChild(root); 89. Node stanza = doc.createElement("Stanza"); 90. root.appendChild(stanza); 91. Node line = doc.createElement("Line"); 92. stanza.appendChild(line); 93. line.appendChild(doc.createTextNode("Once, upon a midnight dreary")); 94. line = doc.createElement("Line"); 95. stanza.appendChild(line); 96. line.appendChild(doc.createTextNode("While I pondered, weak and weary"));
0 0