zoukankan      html  css  js  c++  java
  • Dom4j解析xml文件

    要解析xml文件,首先要看xml文件的节点上是否有xmlns属性(命名空间)

    若有,则需要重写,覆盖。http://www.xuebuyuan.com/1760236.html建议使用第二种方法。

    也有可能会遇到jaxen版本问题造成xpath工作不正常。(百度即可)

    Dom4j和xpath结合选取xml中任意深度的(即所有的)特定元素

    http://www.cnblogs.com/Payne-Wang/archive/2012/12/27/Dom4j-and-XPath.html

    Dom4j和xpath的基础知识http://www.cnblogs.com/forlina/archive/2011/06/09/2076534.html

    代码如下:

    package com.ts.demx.util.transfer;
    
    /**
     * Created by zhxn on 2017/5/5.
     */
    
    import java.awt.print.Book;
    import java.io.File;
    
    import java.util.*;
    
    
    import com.google.common.base.Strings;
    import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    
    public class XmlUtils {
      private Document doc;
      private boolean isHasNamespace;
      private String prefix;
    
      //解析xml文件
      public XmlUtils(String pathname) {
        if (!Strings.isNullOrEmpty(pathname)) {
          // 创建SAXReader的对象saxReader
          SAXReader saxReader = new SAXReader();
          //将pathname转为文件形式
          File file = new File(pathname);
               /*
                  //判断是否有命名空间namespace
                 //覆盖xmlns
                //doc= null;
                Map map=new HashMap();
                map.put("xmlns","http://sarasvati.googlecode.com/ProcessDefinition");
                saxReader.getDocumentFactory().setXPathNamespaceURIs(map);*/
          try {
            //读取xml文件 转换成Document对象  
            this.doc = saxReader.read(file);
          } catch (DocumentException e) {
            e.printStackTrace();
          }
                /*//process根节点
                Element process = doc.getRootElement();
                String url =  process.getNamespaceURI();
                System.out.println(url);*/
        }
      }
    
      //对selectNodes方法进行封装
      public List<Object> getElements(String parserList) {
        //SelectNodes 使用 XPath 来选取节点
        return doc.selectNodes(parserList);
      }
    
      /**
       * 读取xml文件,返回节点信息
       *
       * @param parserList 条件
       * @return listStr 结果
       */
      public List<String> getAttributes(String parserList) {
    
        List<String> listStr = new ArrayList();
        //判断是否为空
        if (!Strings.isNullOrEmpty(parserList)) {
          List<Object> list = doc.selectNodes(parserList);
          // System.out.println(list.size());
          //迭代
          Iterator it = list.iterator();
          while (it.hasNext()) {
            //得到Attribute对象
            Attribute node = (Attribute) it.next();
            //System.out.println(node.getValue());
            //将所需要属性的value值存到listStr里
            listStr.add(node.getValue());
          }
        }
        System.out.println(listStr);
        return listStr;
      }
    
      //test
      public static void main(String[] args) {
        XmlUtils run = new XmlUtils("D:\workspace\demx-emt-stop\demxStore\fileStore\process\model\20150011\20150011.mxml");
        run.getAttributes("//element[@schema='Simple task']/@uid");
      }
    }
  • 相关阅读:
    前后端项目结构规范性记录
    开发问题记录(这部分还是比较零碎)
    对HashMap的一次记录
    面试问题记录 三 (JavaWeb、JavaEE)
    面试问题记录 二 (数据库、Linux、Redis)
    面试问题记录 一 (基础部分)
    对正则表达式的一些记录
    WEB与游戏开发的一些区别
    MarkDown常用语法全纪录
    MySQL压测相关内容
  • 原文地址:https://www.cnblogs.com/zhxn/p/7026649.html
Copyright © 2011-2022 走看看