这是我要解析的Xml
</Worker><Worker>
<Employee_ID>12349294</Employee_ID>
<Position_ID>50099501</Position_ID>
<Process>
<Bp_Type>Hire</Bp_Type>
<Status>Pass</Status>
</Process>
<Process>
<Bp_Type>Leave of Absence</Bp_Type>
<Status>Fail</Status>
<Fault_String>Validation error occurred. Invalid ID value. 'LEAVE_TYPE_ACCOMODATION_ACCOMODATION' is not a valid ID value for type = 'Leave_of_Absence_Type_ID'</Fault_String>
</Process>
<Process>
<Bp_Type>Non Effective Dated Worker Additional Data</Bp_Type>
<Status>Pass</Status>
</Process>
</Worker><Worker>
<Worker>
<Employee_ID>12349386</Employee_ID>
<Position_ID>50279490</Position_ID>
<Process>
<Bp_Type>Hire</Bp_Type>
<Status>Fail</Status>
<Fault_String>Validation error occurred. You can't assign a Military Status to a worker when their Hire Date is before the Effective Date of the Location</Fault_String>
</Process>
/Worker>
这里我试图解析基于Status的xml文件。我需要收集所有“失败”状态,这是我在下面的代码中检查。它正确地为第二个节点工作。但它不适用于存在两个子节点的第一个节点。任何建议都将是完整的帮助
for(int i=0;i<sourceDir.length;i++){
Document doc = docBuilder.parse(sourceDir[i]);
// normalize text representation
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Worker");
int totalPersons = nList.getLength();
for (int s = 0; s < nList.getLength(); s++) {
Node nNode = nList.item(s);
Element eElement = (Element) nNode;
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
if( eElement.getElementsByTagName("Status")!=null && eElement.getElementsByTagName("Status").item(0)!=null &&eElement.getElementsByTagName("Status").item(0).getTextContent().equals("Fail") ){
NodeList nEmp = eElement.getElementsByTagName("Employee_ID");
Element eId = (Element) nEmp.item(0);
NodeList textFNList = eId.getChildNodes();
System.out.println("Employee_ID : "+ ((Node) textFNList.item(0)).getNodeValue().trim());
empID.add(((Node) textFNList.item(0)).getNodeValue().trim());
NodeList nStatus = eElement.getElementsByTagName("Status");
Element eStatus = (Element) nStatus.item(0);
NodeList textLNList = eStatus.getChildNodes();
System.out.println("Status : "+ ((Node) textLNList.item(0)).getNodeValue().trim());
status.add(((Node) textLNList.item(0)).getNodeValue().trim());
NodeList nFault = eElement.getElementsByTagName("Fault_String");
Element eFault = (Element) nFault.item(0);
NodeList textAgeList = eFault.getChildNodes();
System.out.println("Fault_String"+ ((Node) textAgeList.item(0)).getNodeValue().trim());
fault_String.add(((Node) textAgeList.item(0)).getNodeValue().trim());
}
}
}
}
答案 0 :(得分:-1)
您好,您可以按照以下代码获得答案。此代码将解决您的问题
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File("FileLocation"));
doc.getDocumentElement().normalize();
System.out.println("Root element :"
+ doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("Worker");
int totalPersons = nList.getLength();
for (int s = 0; s < nList.getLength(); s++) {
Node nNode = nList.item(s);
Element eElement = (Element) nNode;
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
NodeList processesList = eElement.getElementsByTagName("Process"); for (int i = 0; i < processesList.getLength(); i++) {
Node node = processesList.item(i);
Element element = (Element) node;
if( element.getElementsByTagName("Status")!=null && element.getElementsByTagName("Status").item(0)!=null
&&element.getElementsByTagName("Status").item(0).getTextContent().equals("Fail") ){
NodeList nEmp = eElement.getElementsByTagName("Employee_ID");
Element eId = (Element) nEmp.item(0);
NodeList textFNList = eId.getChildNodes();
System.out.println("Employee_ID : "+ ((Node) textFNList.item(0)).getNodeValue().trim());
empID.add(((Node) textFNList.item(0)).getNodeValue().trim());
NodeList nStatus = element.getElementsByTagName("Status");
Element eStatus = (Element) nStatus.item(0);
NodeList textLNList = eStatus.getChildNodes();
System.out.println("Status : "+ ((Node) textLNList.item(0)).getNodeValue().trim());
status.add(((Node) textLNList.item(0)).getNodeValue().trim());
NodeList nFault = element.getElementsByTagName("Fault_String");
Element eFault = (Element) nFault.item(0);
if(eFault != null){
NodeList textAgeList = eFault.getChildNodes();
System.out.println("Fault_String"+ ((Node) textAgeList.item(0)).getNodeValue().trim());
fault_String.add(((Node) textAgeList.item(0)).getNodeValue().trim());
}else{
fault_String.add("");
}
}
}
}
}
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}