XML之JAXP解析

前端之家收集整理的这篇文章主要介绍了XML之JAXP解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JAXP:是sun公司开发的一套对xml的解析

它由javax.xml.parsers 、org.w3c.dom 、org.xml.sax包及其子包组成


JAXP有2种解析方式:


一、dom解析:将文档读取进内存,生产Document对象

二、sax解析:从上往下读,读取一行处理一行


dom优点:对文档CRUD方便

缺点:内存消耗大


sax优点:内存消耗小,解析速度快、适合读取文档

缺点:CUD不方便


实例xml文档

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
  2. <书>
  3. <书名 id="1234">.Net</书名>
  4. <作者>wht</作者>
  5. </书>
  6. <书>
  7. <书名>javaEE</书名>
  8. <作者>www</作者>
  9. </书>
  10. </书架>


dom解析步骤

  1. 创建dom工厂 DocumentBuilderFactory
  2. 得到dom解析器 DocumentBuilder
  3. 读取xml文档,得到 Document
  1. package Jaxp.dom;
  2.  
  3. import javax.xml.parsers.DocumentBuilder;
  4. import javax.xml.parsers.DocumentBuilderFactory;
  5. import javax.xml.transform.Transformer;
  6. import javax.xml.transform.TransformerFactory;
  7. import javax.xml.transform.dom.DOMSource;
  8. import javax.xml.transform.stream.StreamResult;
  9.  
  10. import org.junit.Test;
  11. import org.w3c.dom.Document;
  12. import org.w3c.dom.Element;
  13. import org.w3c.dom.Node;
  14. import org.w3c.dom.NodeList;
  15.  
  16. public class Dom {
  17.  
  18. //读取文档中的一个指定标签
  19. @Test
  20. public void read1() throws Exception{
  21. //1、创建dom工厂
  22. DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
  23. //2、创建解析器
  24. DocumentBuilder builder=factory.newDocumentBuilder();
  25. //3、读取xml文件 产生Document对象
  26. Document document=builder.parse("src/book.xml");
  27. Node node=document.getElementsByTagName("书").item(0);
  28. System.out.println(node.getNodeName());
  29. }
  30. //读取整个文档的标签
  31. @Test
  32. public void read2() throws Exception{
  33. //1、创建dom工厂
  34. DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
  35. //2、创建解析器
  36. DocumentBuilder builder=factory.newDocumentBuilder();
  37. //3、读取xml文件 产生Document对象
  38. Document document=builder.parse("src/book.xml");
  39.  
  40. Node root=document.getElementsByTagName("书架").item(0);
  41. list(root);
  42. }
  43.  
  44.  
  45. private void list(Node node) {
  46. // TODO Auto-generated method stub
  47. System.out.println(node.getNodeName());
  48. NodeList nodes=node.getChildNodes();
  49. for(int i=0;i<nodes.getLength();i++){
  50. Node node1=nodes.item(i);
  51. list(node1);
  52. }
  53. }
  54. //添加一个标签
  55. @Test
  56. public void add() throws Exception{
  57. //1、创建dom工厂
  58. DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
  59. //2、创建解析器
  60. DocumentBuilder builder=factory.newDocumentBuilder();
  61. //3、读取xml文件 产生Document对象
  62. Document document=builder.parse("src/book.xml");
  63.  
  64. Element element=document.createElement("售价");
  65. element.setTextContent("55元");
  66. Element book=(Element) document.getElementsByTagName("书").item(0);
  67. book.appendChild(element);
  68. TransformerFactory tFactory=TransformerFactory.newInstance();
  69. Transformer transformer=tFactory.newTransformer();
  70. transformer.transform(new DOMSource(document),new StreamResult("src/book.xml"));
  71. }
  72. //删除一个标签
  73. @Test
  74. public void delete() throws Exception{
  75. //1、创建dom工厂
  76. DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
  77. //2、创建解析器
  78. DocumentBuilder builder=factory.newDocumentBuilder();
  79. //3、读取xml文件 产生Document对象
  80. Document document=builder.parse("src/book.xml");
  81. Element book=(Element) document.getElementsByTagName("书").item(0);
  82. book.removeChild(document.getElementsByTagName("售价").item(0));
  83. TransformerFactory tFactory=TransformerFactory.newInstance();
  84. Transformer transformer=tFactory.newTransformer();
  85. transformer.transform(new DOMSource(document),new StreamResult("src/book.xml"));
  86. }
  87. }


sax解析步骤
  1. 创建sax工厂 SAXParserFactory
  2. 得到sax解析器 SAXParser
  3. 得到读取器 XMLReader
  4. 设置内容处理处理器 reader.setContentHandler()
  5. 读取xml文档
  1. package Jaxp.sax;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.xml.parsers.ParserConfigurationException;
  6. import javax.xml.parsers.SAXParser;
  7. import javax.xml.parsers.SAXParserFactory;
  8.  
  9. import org.xml.sax.Attributes;
  10. import org.xml.sax.ContentHandler;
  11. import org.xml.sax.Locator;
  12. import org.xml.sax.SAXException;
  13. import org.xml.sax.XMLReader;
  14.  
  15. public class Demo {
  16.  
  17. public static void main(String[] args) throws ParserConfigurationException,SAXException,IOException {
  18. //1、创建sax工厂
  19. SAXParserFactory factory=SAXParserFactory.newInstance();
  20. //2、得到解析器
  21. SAXParser saxParser=factory.newSAXParser();
  22. //3、得到读取器
  23. XMLReader reader=saxParser.getXMLReader();
  24. //4、设置内容处理器
  25. reader.setContentHandler(new XMLHandler());
  26. //5、读取xml文档
  27. reader.parse("src/book.xml");
  28. }
  29. }
  30.  
  31.  
  32. class XMLHandler implements ContentHandler{
  33.  
  34. @Override
  35. public void startElement(String uri,String localName,String qName,Attributes atts) throws SAXException {
  36. // TODO Auto-generated method stub
  37. System.out.println("<"+qName+">");
  38. for(int i=0;i<atts.getLength();i++){
  39. System.out.println(atts.getQName(i));
  40. System.out.println(atts.getValue(i));
  41. }
  42. }
  43.  
  44. @Override
  45. public void endElement(String uri,String qName)
  46. throws SAXException {
  47. // TODO Auto-generated method stub
  48. System.out.println("</"+qName+">");
  49. }
  50.  
  51. @Override
  52. public void characters(char[] ch,int start,int length)
  53. throws SAXException {
  54. // TODO Auto-generated method stub
  55. System.out.println(new String(ch,start,length));
  56. }
  57. @Override
  58. public void setDocumentLocator(Locator locator) {
  59. // TODO Auto-generated method stub
  60. }
  61.  
  62. @Override
  63. public void startDocument() throws SAXException {
  64. // TODO Auto-generated method stub
  65. }
  66.  
  67. @Override
  68. public void endDocument() throws SAXException {
  69. // TODO Auto-generated method stub
  70. }
  71.  
  72. @Override
  73. public void startPrefixMapping(String prefix,String uri)
  74. throws SAXException {
  75. // TODO Auto-generated method stub
  76. }
  77.  
  78. @Override
  79. public void endPrefixMapping(String prefix) throws SAXException {
  80. // TODO Auto-generated method stub
  81. }
  82.  
  83.  
  84. @Override
  85. public void ignorableWhitespace(char[] ch,int length)
  86. throws SAXException {
  87. // TODO Auto-generated method stub
  88. }
  89.  
  90. @Override
  91. public void processingInstruction(String target,String data)
  92. throws SAXException {
  93. // TODO Auto-generated method stub
  94. }
  95.  
  96. @Override
  97. public void skippedEntity(String name) throws SAXException {
  98. // TODO Auto-generated method stub
  99. }
  100. }

sun公司提供了DefaultHandler

  1. package Jaxp.sax;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.xml.parsers.ParserConfigurationException;
  6. import javax.xml.parsers.SAXParser;
  7. import javax.xml.parsers.SAXParserFactory;
  8.  
  9. import org.xml.sax.Attributes;
  10. import org.xml.sax.ContentHandler;
  11. import org.xml.sax.Locator;
  12. import org.xml.sax.SAXException;
  13. import org.xml.sax.XMLReader;
  14. import org.xml.sax.helpers.DefaultHandler;
  15.  
  16. public class Demo2 {
  17.  
  18. public static void main(String[] args) throws ParserConfigurationException,IOException {
  19. //1、创建sax工厂
  20. SAXParserFactory factory=SAXParserFactory.newInstance();
  21. //2、得到解析器
  22. SAXParser saxParser=factory.newSAXParser();
  23. //3、得到读取器
  24. XMLReader reader=saxParser.getXMLReader();
  25. //4、设置内容处理器
  26. reader.setContentHandler(new TagValue());
  27. //5、读取xml文档
  28. reader.parse("src/book.xml");
  29. }
  30. }
  31.  
  32. //获取第二个 作者的名字
  33. class TagValue extends DefaultHandler{
  34.  
  35. private String currentTag;
  36. private int currentNum;
  37. private int needTagNum=2;
  38. @Override
  39. public void startElement(String uri,Attributes attributes) throws SAXException {
  40. currentTag=qName;
  41. if(currentTag.equals("作者")){
  42. currentNum++;
  43. }
  44. }
  45. @Override
  46. public void characters(char[] ch,int length)
  47. throws SAXException {
  48. if("作者".equals(currentTag)&¤tNum==needTagNum){
  49. System.out.println(new String(ch,length));
  50. }
  51. }
  52.  
  53. @Override
  54. public void endElement(String uri,String qName)
  55. throws SAXException {
  56. currentTag=null;
  57. }
  58.  
  59.  
  60.  
  61. }

猜你在找的XML相关文章