摘要:java and xml parse
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="6.9">
<file name="demo-demo/java/Test.java">
<error line="5" column="5" severity="warning" message="Missing a Javadoc comment." source="JavadocMethodCheck">
showMessage
</error>
</file>
</checkstyle>
String fileUrl = "D:\\workspace_alm\\checkStyleValify\\PMD-Reports.xml";
File xmlFile = new File(fileUrl);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("file");
NamedNodeMap nnm;
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("name: " + eElement.getAttribute("name"));
NodeList conditionList = eElement.getElementsByTagName("error");
for (int k = 0; k < conditionList.getLength(); ++k)
{
Element condition = (Element) conditionList.item(k);
System.out.println("severity: " + condition.getAttribute("severity"));
System.out.println("message: " + condition.getAttribute("message"));
String conditionText = condition.getFirstChild().getNodeValue();
System.out.println("message===================" + conditionText);
}
}
}
REF:http://stackoverflow.com/questions/6604876/parsing-xml-with-nodelist-and-documentbuilder