У меня есть следующий код:

https://code.sololearn.com/c9RLoBhB7Mua

public class Program {

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

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

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

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

    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();
                        } else if (measElement.getAttribute("LOC").equals("RIGHT")) {
                            frontRight1 = childElement.getTextContent();
                        }
                    }
                    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();
                        } else if (measElement.getAttribute("LOC").equals("RIGHT")) {
                            frontRight2 = childElement.getTextContent();
                        }
                    }

                 }
            } 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();
                        } else if (measElement.getAttribute("LOC").equals("RIGHT")) {
                            rearRight1 = childElement.getTextContent();
                        }
                    }
                    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();
                        } else if (measElement.getAttribute("LOC").equals("RIGHT")) {
                            rearRight2 = childElement.getTextContent();
                        }
                    }

                }
            }
        }

        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

Я явно что-то упускаю в цикле, но не могу понять, что именно. Я перебираю AXLE 1, а затем думаю, что делаю то же самое с AXLE 2, но похоже, что AXLE 2 записывает поверх AXLE 1.

< Сильный > XML:

<DiagnosticResult>
   <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">5.871</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">5.694</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.821</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">6.052</VALUE>
         <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/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">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">8/32</VALUE>
      </MEAS>
   </SECTION>
</DiagnosticResult>
0
A.J 6 Май 2020 в 22:07

2 ответа

Вы извлекаете теги "MEAS" для всего документа.

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

Вы можете переместить эту строку и отредактировать ее, чтобы ограничить поиск одним разделом за раз:

    for (int i = 0; i < nodeListSection.getLength(); i++) {
        Element sectionElement = (Element) nodeListSection.item(i);
        NodeList nodeListMeas = sectionElement.getElementsByTagName("MEAS");
1
PotatoesMaster 6 Май 2020 в 20:02

В конечном счете, проблема заключается в том, что вместо этого нужно делать следующее в каждом цикле, а не в строке перед строкой 33 (NodeList nodeListSection = document.getElementsByTagName ("SECTION");)

    NodeList nodeListMeas = sectionElement.getElementsByTagName("MEAS");

Кроме того, обратите внимание, что я все еще получаю вопрос NodeList, но вместо этого получаю его из соответствующего элемента sectionElement.

1
Albert Turri 6 Май 2020 в 20:02