JAVA用DOM方式讀取xml檔案
Status.xml
<?xml version="1.0" encoding="UTF-8"?>
<StatuList>
<Statu id="1">
<id>1</id>
<name>待機</name>
<status>00H</status>
</Statu>
<Statu id="2">
<id>2</id>
<name>停機</name>
<status>01H</status>
</Statu>
<Statu id="3">
<id>3</id>
<name>啟動</name>
<status>02H</status>
</Statu>
</StatuList>
解析xml程式碼如下:
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
/**
* 用DOM方式讀取xml檔案
* @author lune
*/
public class ReadxmlByDom {
private static DocumentBuilderFactory dbFactory = null;
private static DocumentBuilder db = null;
private static Document document = null;
private static List<Map<String, Object>> status = null;
static{
try {
dbFactory = DocumentBuilderFactory.newInstance();
db = dbFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
public Document getDocument(String url) throws DocumentException{
SAXReader reader = new SAXReader();
InputStream in = getClass().getClassLoader().getResourceAsStream(url);//讀取檔案流,Url為controller.xml檔案
//File file=new File(url);
Document document = (Document) reader.read(in);//獲得檔案例項
return document;
}
public static List<Map<String,Object>> getStatus(String fileName) throws Exception{
//將給定 URI 的內容解析為一個 XML 文件,並返回Document物件
document = db.parse(fileName);
//按文件順序返回包含在文件中且具有給定標記名稱的所有 Element 的 NodeList
NodeList bookList = document.getElementsByTagName("Statu");
status = new ArrayList<Map<String,Object>>();
//遍歷status
for(int i=0;i<bookList.getLength();i++){
Map<String,Object> statu = new HashMap<String,Object>();
//獲取第i個statu結點
org.w3c.dom.Node node = bookList.item(i);
//獲取第i個statu的所有屬性
NamedNodeMap namedNodeMap = node.getAttributes();
//獲取已知名為id的屬性值
String id = namedNodeMap.getNamedItem("id").getTextContent();//System.out.println(id);
statu.put("id", Integer.parseInt(id));
//獲取statu結點的子節點,包含了Test型別的換行
NodeList cList = node.getChildNodes();//System.out.println(cList.getLength());9
//將一個statu裡面的屬性加入陣列
ArrayList<String> contents = new ArrayList<>();
for(int j=1;j<cList.getLength();j+=2){
org.w3c.dom.Node cNode = cList.item(j);
String content = cNode.getFirstChild().getTextContent();
contents.add(content);
}
statu.put("name", contents.get(1));
statu.put("status", contents.get(2));
status.add(statu);
}
return status;
}
public static void main(String[] args) {
try {
System.out.println(getStatus("src/com/beidou/xml/parser/Status.xml"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
輸出結果:[{id=1, status=00H, name=待機}, {id=2, status=01H, name=停機}, {id=3, status=02H, name=啟動}]
備註:部署到linux伺服器上時,檔案地址可寫這樣:
String path = getClass().getClassLoader().getResource("/com/beidou/xml/parser/Status.xml").getPath();