解析xml

来源:互联网 发布:相册书制作软件 编辑:程序博客网 时间:2024/06/01 18:38
1.何为XML解析?

   所谓xml解析就是采用一定的技术对xml文档中
   描述的数据进行提取。

  2.常用的XML解析方案?
   1)dom 解析
   2)dom4j解析
   3)pull解析
   4)sax解析
   5)................


1.XML DOM 解析 概述

   所谓dom解析是将整个xml文档读到内存,在内存
   构建一颗倒置的节点(Node)树,然后再从这颗树中
   提取我们需要的数据。

   dom解析是w3c的一个标准,是常用解析方案之一。
   适用于一些比较小的xml文档的解析。
   
   优点:标准,简单,重复读取及修改xml文档相对方便
   缺点:对于比较大的xml文档可能会影响系统性能。

  2.XML DOM 解析步骤

   1)构建解析器对象(DocumentBuilder)
   2)解析xml文件获得document对象
   3)处理数据(一般从根元素开始)

-------------------------------------------------------------------------------------------------------------------------------------------
解析的几个案例

重点: 这个案例是修改字符串,转换成xml格式的文件
   static String stringStr =
   "<books>" +
   "<book>ios</book>"+
   "</books>";
 public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException,       TransformerFactoryConfigurationError, TransformerException  {
  //构建解析器对象
  DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  //创建一个Document对象
  Document document = builder.parse(new InputSource(new StringReader(stringStr)));
  //创建一个新的节点
  Element element = document.createElement("book");
  //创建一个文本节点
  Text newChild = document.createTextNode("Android");
  //向元素几点中追加文本节点
  element.appendChild(newChild);
  //在根节点后面追加子节点
  document.getDocumentElement().appendChild(element);
  
  //转换,把字符串转换成xml文件,创建一个转换对象
  Transformer transformer = TransformerFactory.newInstance().newTransformer();
  //设置特性
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  transformer.transform(new DOMSource(document), new StreamResult(new File("date.xml")));
  
 }

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2
重点: 这个案例是获取字符串的节点
 static String stringStr =
   "<books>" +
   "<book><id>101</id><name>Android</name></book>"+
   "<book><id>102</id><name>IOS</name></book>"+
   "</books>";

 static List<Map<String, String>> list = new ArrayList<>();
 public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException  {
  //构建解析器对象
  DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  //获得根元素对象的两种方式
//  Document document = builder.parse(new ByteArrayInputStream(stringStr.getBytes()));
  Document document = builder.parse(new InputSource(new StringReader(stringStr)));
  //获得根元素节点
  Element element = document.getDocumentElement();

  System.out.println(element.getNodeName());
  //获得所有子元素节点
  NodeList childNodes = element.getChildNodes();
  //循环每个子元素节点
  for (int i = 0; i < childNodes.getLength(); i++) {
   //获得每个子元素节点并接收
   Node node = childNodes.item(i);
   
   list.add(getMap(node));
  }
  System.out.println(list);
 }
 static Map<String, String> getMap(Node node){
  NodeList childNodes = node.getChildNodes();
  Map<String, String> map = new HashMap<>();
  for (int i = 0; i < childNodes.getLength(); i++) {
   Node node2 = childNodes.item(i);
   //getFirstChiled()这个方法是获得他的孩子节点;
   map.put(node2.getNodeName(), node2.getFirstChild().getNodeValue());
  }
  return map;
 }

-----------------------------------------------------------------------------------------------------------------------------------
这个是最基本的案例,解析xml文件
  // 创建解析器对象
  DocumentBuilder builder = DocumentBuilderFactory.
    newInstance().newDocumentBuilder();
  //解析文件,找到文件文职,获得Document代表文档的对象
  Document document = builder.parse(new File("src/day18/Element.xml"));
  //处理数据,获得文档的根元素,Element;
  Element element = document.getDocumentElement();
  //获得根元素当中的子元素
  NodeList list = element.getChildNodes();
  //循环获得所有根元素当中的子元素节点
  for (int i = 0; i < list.getLength(); i++) {
   //这个是获得所有子元素的节点
   Node node = list.item(i);
   //这个判断是去所有的元素节点
   if (node.getNodeType()==Node.ELEMENT_NODE) {
    System.out.println(node.getNodeName());
    //调用获得子元素节点当中的属性值
    nodeAttr(node);
   }
  }
  //这个是获得根元素的名字。
  System.out.println(element.getNodeName());
  
  
 }
 /**获得Node节点的属性*/
 static void nodeAttr(Node node){
  //获得节点的属性
  NamedNodeMap attributes = node.getAttributes();
  //遍历所有子节点元素获得节点、
  for (int i = 0; i < attributes.getLength(); i++) {
   Node node2 = attributes.item(i);
   //获得所有子元素节点的属性值
   System.err.println(node2.getNodeName()+"="+node2.getNodeValue());
  }
 }


0 0