Цикл по NodeList Java - PullRequest
       104

Цикл по NodeList Java

0 голосов
/ 06 мая 2020

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

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

Мне явно чего-то не хватает в l oop, но я не могу понять, что точно. Я повторяю 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>

Ответы [ 2 ]

1 голос
/ 06 мая 2020

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

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

Также обратите внимание, что я все еще получаю рассматриваемый NodeList, но вместо этого получаю его из соответствующего 'sectionElement'.

1 голос
/ 06 мая 2020

Вы получаете теги «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");
...