Document document = saxReader.read(new File(filename));&l">

从硬盘上读取xml文件并解析

80酷酷网    80kuku.com

  xml|硬盘

SAXReader saxReader = new SAXReader();
  String filename = "D:\\补单据整理数据\\builderXML\\2101invdoc.xml";
   Document document = saxReader.read(new File(filename));
   CharArrayWriter out = new CharArrayWriter();
   OutputFormat format = OutputFormat.createPrettyPrint();
   XMLWriter write = new XMLWriter(out, format);
   write.write(document);
   String s = out.toString();

//输出到控制台

   System.out.println(s);

 

//顾名思义这个方法是取得Attribute的

public void getattribute(Element element) {
  for (Iterator j = element.attributeIterator(); j.hasNext();) {
   Attribute attribute = (Attribute) j.next();
   //这里写上相应的动作即可

  }

 }

//取得其它节点的方法和取Attribute的很像,把节点类型换成Element就ok了


 



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: