public void readXML(String filePath) throws Exception {
Document doc = getDocument(filePath);
Element el = doc.getDocumentElement();
NodeList nodes = el.getChildNodes();
return xmlReader(nodes, space);
}
public Document getDocument(String file_path) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(file_path);
Document doc = builder.parse(inputStream);
return doc;
}
public static void xmlReader(NodeList nodes, String indent) throws Exception {
int len = nodes.getLength();
for (int i = 0; i < len; i++) {
Node child = nodes.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
if (child.hasAttributes()) {
NamedNodeMap attrs = child.getAttributes();
for (int j = 0; j < attrs.getLength(); j++) {
System.out.println(attrs.item(j).getNodeName()); System.out.println(attrs.item(j).getNodeValue());
}
}
}
if (child.hasChildNodes()) {
xmlReader(child.getChildNodes(), " " + indent);
}
}
}
No comments:
Post a Comment