JavaでDOMを使用してxmlを読み取るサンプル
Javaコード:
public static void main(String[] args) throws Exception {
// Document doc =
// DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder().newDocument();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder
.parse(“E:\\startnews24Project\\tts_tuan\\src\\dom_test\\java\\com\\startnews24\\dom_test\\dom_dom_test.xml");
NodeList nodeList = doc.getElementsByTagName(“data");
for (int i = 0; i < nodeList.getLength(); i++) {
Element ele = (Element)nodeList.item(i);
NodeList list = ele.getChildNodes();
for(int j =0;j<list.getLength();j++){
Node node = list.item(j);
System.out.println(“-:" + node.getNodeName());
System.out.println(“–:" + node.getTextContent());
}
}
}