您的位置:首页 > 其它

读取xml

2015-08-22 15:28 176 查看

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class ReadXml {

 public static void main(String[] args) throws DocumentException {
  File file = new File("E:/workspace3.7/com.huawei.jing/src/a.xml");
  SAXReader reader = new SAXReader();
  Document doc = null;
  doc = reader.read(file);
  
  @SuppressWarnings("unchecked")
  List<Node> apiNodes = doc.selectNodes("factory/apple");
  for(Node node : apiNodes)
  {
   String apple = node.selectSingleNode("@name").getText().trim();
   String module = node.getText().trim();
   System.out.println(apple + ":apple___module:"+module);
  }
  
 }

}

a.xml

<?xml version="1.0" encoding="UTF-8"?>
<factory>
 <apple name="one">red,green</apple>
 <apple name="two">red,green,yellow</apple>
</factory>

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: