我是使用 DOM 进行 XML JAVA 解析的新手,并且构建了一个小型解析器来读取具有或多或少标准结构的应用程序 XML 文件。XML 文件提取如下所示:
*<?xml version="1.0" encoding="UTF-8"?>
<applications>
<application name="CustomerApplications/TEST3/">
<service name="INFileAdapterConfiguration.aar">
<deploymentStatus>Success</deploymentStatus>
<serviceInstance name="TEST3">
<machine>test</machine>
<status>Unknown. HawkAgent on test is not responding</status>
</serviceInstance>
</service>
<service name="OUTFileAdapterConfiguration.aar">
<deploymentStatus>Success</deploymentStatus>
<serviceInstance name="TEST3">
<machine>test</machine>
<status>Unknown. HawkAgent on test is not responding</status>
</serviceInstance>
</service>
</application>
<application name="TEST2">
<deploymentStatus>Undeployed</deploymentStatus>
</application>
</applications>
我的目标是根据名为"deploymentStatus"的特定元素的值过滤结果:如果元素值设置为"成功",那么我仍然会打印子节点和属性,但如果它是"成功"以外的任何其他值,那么我只会打印出类似"应用程序未部署"的内容,而不是读取子节点。
目前我能够获取所有应用程序名称,但是在最终结果中,我丢失了其他节点和子值(部署状态、服务实例、计算机、状态)。解析器代码如下:
*import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Hrb_auto {
public static void main(String[] args) {
// TODO Auto-generated method stub
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document= builder.parse(new File ("C:\Temp\hrb_check \AppStatus.xml"));
final Element racine = document.getDocumentElement();
final NodeList racineNoeuds = racine.getChildNodes();
final int nbRacineNoeuds = racineNoeuds.getLength();
for (int i = 0; i<nbRacineNoeuds; i++) {
if(racineNoeuds.item(i).getNodeType() == Node.ELEMENT_NODE) {
final Element application = (Element) racineNoeuds.item(i);
System.out.println("n Application name: " + application.getAttribute("name"));
final NodeList AppNoeuds = application.getChildNodes();
final int nbAppNoeuds = AppNoeuds.getLength();
for (int j = 0; j<nbAppNoeuds; j++) {
if (AppNoeuds.item(j).getNodeType() == Node.ELEMENT_NODE) {
final Element service = (Element) AppNoeuds.item(j);
if (service.hasAttribute("name")) {
System.out.print("t Service name: " + service.getAttribute("name"));
final Element deploymentStatus = (Element) service.getElementsByTagName("deploymentStatus").item(0);
final Element serviceInstance = (Element) service.getElementsByTagName("serviceInstance").item(0);
if (deploymentStatus.getTextContent() == "Success") {
System.out.print("t deploymentStatus: " + deploymentStatus.getTextContent());
System.out.print("t Service Instance: " + serviceInstance.getAttribute("name"));
final Element machine = (Element) serviceInstance.getElementsByTagName("machine").item(0);
final Element status = (Element) serviceInstance.getElementsByTagName("status").item(0);
System.out.print("t machine: " + machine.getTextContent());
System.out.println("t status: " + status.getTextContent());
} else {
System.out.println("Service disabled, no service to be checked");
}
}
else {
System.out.println("Application undeployed, no service to be checked");
}
}
}
}
}
}
catch (final ParserConfigurationException e) {
e.printStackTrace();
}
catch (final SAXException e) {
e.printStackTrace();
}
catch (final IOException e) {
e.printStackTrace();
}
}
}
获得的结果:
*Application name: CustomerApplications/TEST3/TEST
Service name: INFileAdapterConfiguration.aarService disabled, no service to be checked
Service name: RTCISOutFileAdapterConfiguration.aarService disabled, no service to be checked*
预期成果:
*Application name: CustomerApplications/TEST3/TEST
Service name: INFileAdapterConfiguration.aar deploymentStatus: Success Service Instance: TEST3 machine: test status: Unknown. HawkAgent on test is not responding
Service name: OutFileAdapterConfiguration.aar deploymentStatus: Success Service Instance: TEST3 machine: test status: Unknown. HawkAgent on test is not responding*
提供的任何帮助都会很棒!
既然你写了"提供任何帮助......",我建议使用我所属的库。如果没有XML示例,我无法弄清楚所有细节,但是您的解决方案可能如下所示:
public class Hrb_auto {
public interface Service {
@XBRead("./@name")
String getServiceName();
@XBRead("./deploymentStatus")
String getDeploymentStatus();
@XBRead("./serviceInstance/@name")
String getServiceInstance();
@XBRead("./serviceInstance/machine")
String getMachine();
@XBRead("./serviceInstance/status")
String getStatus();
}
public interface Application {
@XBRead("./@name")
String getName();
@XBRead("./service")
List<Service> getServices();
}
public static void main(String[] args) {
List<Application> applications = new XBProjector().io().file("<path to data>").evalXPath("/applications/application").asListOf(Application.class);
if (applications.isEmpty()) {
System.out.println("No applications found");
return;
}
for (Application app:applications) {
System.out.println("Application name: "+app.getName());
if (app.getServices().isEmpty()) {
System.out.println("Application undeployed, no service to be checked");
return;
}
for (Service service:app.getServices()) {
if ("Success".equals(service.getDeploymentStatus())) {
System.out.print("t deploymentStatus: " + service.getDeploymentStatus());
System.out.print("t Service Instance: " + service.getServiceInstance());
System.out.print("t machine: " + service.getMachine());
System.out.println("t status: " + service.getStatus());
continue;
}
System.out.println("Service disabled, no service to be checked");
}
}
}
}
通过创建两个接口"服务"和"应用程序",您可以使用投影仪将它们用作XML数据的面向对象视图。XML 和 Java 之间的映射是通过 XPath 完成的。
优化路径后,该程序会打印出来:
Application name: CustomerApplications/TEST3/
deploymentStatus: Success Service Instance: TEST3 machine: test status: Unknown. HawkAgent on test is not responding
deploymentStatus: Success Service Instance: TEST3 machine: test status: Unknown. HawkAgent on test is not responding
Application name: TEST2
Application undeployed, no service to be checked
这是不正确使用 ==
进行字符串比较的典型示例:
if (deploymentStatus.getTextContent() == "Success") {
...
}
请改用equals
,代码应按预期工作。