Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果DOM XML解析活动中的条件不匹配_Java_Xml_Dom_Domparser - Fatal编程技术网

Java 如果DOM XML解析活动中的条件不匹配

Java 如果DOM XML解析活动中的条件不匹配,java,xml,dom,domparser,Java,Xml,Dom,Domparser,我是一个使用DOM进行XML JAVA解析的新手,并且已经构建了一个小型解析器来读取或多或少具有标准结构的应用程序XML文件。XML文件提取如下所示: *<?xml version="1.0" encoding="UTF-8"?> <applications> <application name="CustomerApplications/TEST3/"> <service name="INFileAdapterConfiguration.aa

我是一个使用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>
预期结果:

*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");
            }
        }
    }
}

这是不正确使用==进行字符串比较的典型示例:

if (deploymentStatus.getTextContent() == "Success") {
    ...
}

请改用equals,您的代码应该可以正常工作。

请提供输入xml的摘录。这将有助于理解您的问题。您好,我添加了要读取的输入xml文件的摘录。对不起,如果添加了示例,您可以重新检查吗?我看不出来。当然…今天不是我的日子:。它没有得到适当的考虑。现在应该可以了。提前感谢您的帮助。您好,我添加了要读取的输入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");
            }
        }
    }
}
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") {
    ...
}