将嵌套的XML映射到Java变量中

问题描述 投票:0回答:1

我有以下XML示例:

<DiagnosticResult>
   <RESULTSHEADER>
      <VEHICLE>
         <IDENT>
            <REGISTRATION>car registration</REGISTRATION>
            <MANUFACTURER>car manuf</MANUFACTURER>
            <MODEL>car model</MODEL>
         </IDENT>
         <DATA>
            <VEHICLE_IMAGE>veh url</VEHICLE_IMAGE>
         </DATA>
      </VEHICLE>
   </RESULTSHEADER>
   <RESULT OBJECT="WHEEL_ALIGNMENT" METHOD="MANUFACTURER_SPECIFIC">
      <SECTION OBJECT="TIRE_INSPECTION" AXLE="1">
         <TITLE>Tire Inspection Axle 1</TITLE>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_1" LOC="LEFT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">4.491</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">6/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_2" LOC="LEFT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.871</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">7/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_3" LOC="LEFT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">6.277</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">8/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_1" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.694</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">7/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_2" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">4.821</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">6/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_3" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">4.560</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">6/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_4" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.158</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">6/32</VALUE>
         </MEAS>
      </SECTION>
      <SECTION OBJECT="TIRE_INSPECTION" AXLE="2">
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_1" LOC="LEFT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.199</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">7/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_2" LOC="LEFT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">6.052</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">8/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_3" LOC="LEFT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">6.275</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">8/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_1" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.270</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">7/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_2" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.249</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">7/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_3" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.685</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">7/32</VALUE>
         </MEAS>
      </SECTION>
      <SUMMARY>
         <TITLE>Quick Check Results</TITLE>
         <MEAS OBJECT="TIRE_CHECK">
            <TITLE>Tread Depth</TITLE>
            <VALUE RESULT="1">PASSED</VALUE>
         </MEAS>
      </SUMMARY>
   </RESULT>
</DiagnosticResult>

之间有更多字段,但是提供的摘录是我感兴趣的。我想基于此XML声明变量。

我完全可以基于标签来获取特定值。例如,注册。

我的代码是:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));

String registration = doc.getElementsByTagName("REGISTRATION").item(0).getTextContent();

我遇到的问题是如何访问具有相同名称的字段。举一个例子:

 <MEAS OBJECT="TREAD_DEPTH_DISPLAY_1" LOC="LEFT">
    <TITLE>Tread Depth Point</TITLE>
    <VALUE UNIT="mm" RESULT="0">4.491</VALUE>
    <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">6/32</VALUE>
 </MEAS>

它在文件中显示四次。在<SECTION OBJECT="TIRE_INSPECTION" AXLE="1">下两次,在<SECTION OBJECT="TIRE_INSPECTION" AXLE="2">下两次。我需要从整个块中获取数据。 result, value, lowlim1

我是否可以使用当前使用的方法来获取这些字段,或者是否需要其他方法?

编辑:

当前代码:https://code.sololearn.com/c9RLoBhB7Mua

public class Program {


    public static String frontLeft1 = "";
    public static String frontLeft1Status = "";
    public static String frontLeft2 = "";
    public static String frontLeft2Status = "";

    public static String frontRight1 = "";
    public static String frontRight1Status = "";
    public static String frontRight2 = "";
    public static String frontRight2Status = "";

    public static String rearLeft1 = "";
    public static String rearLeft1Status = "";
    public static String rearLeft2 = "";
    public static String rearLeft2Status = "";

    public static String rearRight1 = "";
    public static String rearRight1Status = "";
    public static String rearRight2 = "";
    public static String rearRight2Status = "";


    public static void main(String[] args) {

        try {
            String xml = "<DiagnosticResult><REGISTRATION>123123</REGISTRATION><RESULT OBJECT=\"WHEEL_ALIGNMENT\" METHOD=\"MANUFACTURER_SPECIFIC\"><SECTION OBJECT=\"TIRE_INSPECTION\" AXLE=\"1\"><TITLE>Tire Inspection Axle 1</TITLE><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_1\" LOC=\"LEFT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"4/32\" RESULT=\"1\">1/32</VALUE></MEAS><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_2\" LOC=\"LEFT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"4/32\" RESULT=\"1\">2/32</VALUE></MEAS><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_1\" LOC=\"RIGHT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"4/32\" RESULT=\"1\">3/32</VALUE></MEAS><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_2\" LOC=\"RIGHT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"4/32\" RESULT=\"1\">4/32</VALUE></MEAS></SECTION><SECTION OBJECT=\"TIRE_INSPECTION\" AXLE=\"2\"><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_1\" LOC=\"LEFT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">5.199</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"4/32\" RESULT=\"1\">5/32</VALUE></MEAS><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_2\" LOC=\"LEFT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"2/32\" RESULT=\"1\">6/32</VALUE></MEAS><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_1\" LOC=\"RIGHT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"4/32\" RESULT=\"1\">7/32</VALUE></MEAS><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_2\" LOC=\"RIGHT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"1/32\" RESULT=\"1\">8/32</VALUE></MEAS></SECTION></RESULT></DiagnosticResult>";
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        dbFactory.setNamespaceAware(true);
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document document = dBuilder.newDocument();
        document = dBuilder.parse(new ByteArrayInputStream(xml.getBytes()));

        NodeList nodeListMeas = document.getElementsByTagName("MEAS");
        NodeList nodeListSection = document.getElementsByTagName("SECTION");

        for (int i = 0; i < nodeListSection.getLength(); i++) {
            Element sectionElement = (Element) nodeListSection.item(i);
            if (sectionElement.getAttribute("AXLE").equals("1")) {
                for (int j = 0; j < nodeListMeas.getLength(); j++) {
                    Element measElement = (Element) nodeListMeas.item(j);
                    Node node = nodeListMeas.item(j);
                    if (measElement.getAttribute("OBJECT").equals("TREAD_DEPTH_DISPLAY_1")) {
                        NodeList childNodeList = measElement.getElementsByTagName("VALUE");
                        Element childElement = (Element) childNodeList.item(1);
                        if (measElement.getAttribute("LOC").equals("LEFT")) {
                            frontLeft1 = childElement.getTextContent();
                            frontLeft1Status = childElement.getAttribute("RESULT");
                        } else if (measElement.getAttribute("LOC").equals("RIGHT")) {
                            frontRight1 = childElement.getTextContent();
                            frontRight1Status = childElement.getAttribute("RESULT");
                        }
                    }
                    if (measElement.getAttribute("OBJECT").equals("TREAD_DEPTH_DISPLAY_2")) {
                        NodeList childNodeList = measElement.getElementsByTagName("VALUE");
                        Element childElement = (Element) childNodeList.item(1);
                        if (measElement.getAttribute("LOC").equals("LEFT")) {
                            frontLeft2 = childElement.getTextContent();
                            frontLeft2Status = childElement.getAttribute("RESULT");
                        } else if (measElement.getAttribute("LOC").equals("RIGHT")) {
                            frontRight2 = childElement.getTextContent();
                            frontRight2Status = childElement.getAttribute("RESULT");
                        }
                    }

                 }
            } else if (sectionElement.getAttribute("AXLE").equals("2")) {
                for (int j = 0; j < nodeListMeas.getLength(); j++) {
                    Element measElement = (Element) nodeListMeas.item(j);
                    Node node = nodeListMeas.item(j);
                    if (measElement.getAttribute("OBJECT").equals("TREAD_DEPTH_DISPLAY_1")) {
                        NodeList childNodeList = measElement.getElementsByTagName("VALUE");
                        Element childElement = (Element) childNodeList.item(1);
                        if (measElement.getAttribute("LOC").equals("LEFT")) {
                            rearLeft1 = childElement.getTextContent();
                            rearLeft1Status = childElement.getAttribute("RESULT");
                        } else if (measElement.getAttribute("LOC").equals("RIGHT")) {
                            rearRight1 = childElement.getTextContent();
                            rearRight1Status = childElement.getAttribute("RESULT");
                        }
                    }
                    if (measElement.getAttribute("OBJECT").equals("TREAD_DEPTH_DISPLAY_2")) {
                        NodeList childNodeList = measElement.getElementsByTagName("VALUE");
                        Element childElement = (Element) childNodeList.item(1);
                        if (measElement.getAttribute("LOC").equals("LEFT")) {
                            rearLeft2 = childElement.getTextContent();
                            rearLeft2Status = childElement.getAttribute("RESULT");
                        } else if (measElement.getAttribute("LOC").equals("RIGHT")) {
                            rearRight2 = childElement.getTextContent();
                            rearRight2Status = childElement.getAttribute("RESULT");
                        }
                    }

                }
            }
        }

        System.out.println(frontLeft1);
        System.out.println(frontLeft2);
        System.out.println(frontRight1);
        System.out.println(frontRight2);
        System.out.println(rearLeft1);
        System.out.println(rearLeft2);
        System.out.println(rearRight1);
        System.out.println(rearRight2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这会查找元素,但会覆盖变量。输出应为:

1/32
2/32
3/32
4/32
5/32
6/32
7/32
8/32

但是现在是:

5/32
6/32
7/32
8/32
5/32
6/32
7/32
8/32
java xml dom
1个回答
1
投票
尝试以下解决方案,
© www.soinside.com 2019 - 2024. All rights reserved.