要读取的XML获取包含特定值-java的子项

时间:2018-03-01 15:34:45

标签: java xmlreader

我无法获得某些特定元素,我有一个xml文件,其根节点“问题”包含子“解决方案”。在解决方案中我有标签名称“cost”,其值为505.9208295302417,我想得到这个孩子的所有元素包含505.9208295302417。到目前为止,我所做的就是所有节点,我只想要这个特定孩子的元素而不是另一个。下面是xml和java代码。

      <problem>    
      <solution>
      <cost>505.9214355631349</cost>
                   <routes>
                        <route>
                             <driverId>noDriver</driverId>
                             <vehicleId>1_1</vehicleId>
                             <start>0.0</start>
                             <act type="service">
                                  <serviceId>5 </serviceId>
                                  <arrTime>109.9819741964403</arrTime>
                                  <endTime>119.9819741964403</endTime>
                             </act>
                             <end>229.9639483928806</end>
                        </route>
                        <route>
                             <driverId>noDriver</driverId>
                             <vehicleId>3_1</vehicleId>
                             <start>0.0</start>
                             <act type="service">
                                  <serviceId>2 </serviceId>
                                  <arrTime>109.98268205388193</arrTime>
                                  <endTime>119.98268205388193</endTime>
                             </act>
                             <act type="service">
                                  <serviceId>1 </serviceId>
                                  <arrTime>119.98357684436793</arrTime>
                                  <endTime>129.98357684436792</endTime>
                             </act>
                             <act type="service">
                                  <serviceId>3 </serviceId>
                                  <arrTime>129.98449911991617</arrTime>
                                  <endTime>139.98449911991617</endTime>
                             </act>
                             <act type="service">
                                  <serviceId>4 </serviceId>
                                  <arrTime>139.98539391040217</arrTime>
                                  <endTime>149.98539391040217</endTime>
                             </act>
                             <end>259.9672978232725</end>
                        </route>
                   </routes>
              </solution>
              <solution>
                   <cost>505.9208295302417</cost>
                   <routes>
                        <route>
                             <driverId>noDriver</driverId>
                             <vehicleId>1_1</vehicleId>
                             <start>0.0</start>
                             <act type="service">
                                  <serviceId>5 </serviceId>
                                  <arrTime>109.9819741964403</arrTime>
                                  <endTime>119.9819741964403</endTime>
                             </act>
                             <end>229.9639483928806</end>
                        </route>
                        <route>
                             <driverId>noDriver</driverId>
                             <vehicleId>3_1</vehicleId>
                             <start>0.0</start>
                             <act type="service">
                                  <serviceId>4 </serviceId>
                                  <arrTime>109.98190391287031</arrTime>
                                  <endTime>119.98190391287031</endTime>
                             </act>
                             <act type="service">
                                  <serviceId>2 </serviceId>
                                  <arrTime>119.98282618841856</arrTime>
                                  <endTime>129.98282618841856</endTime>
                             </act>
                             <act type="service">
                                  <serviceId>1 </serviceId>
                                  <arrTime>129.98372097890456</arrTime>
                                  <endTime>139.98372097890456</endTime>
                             </act>
                             <act type="service">
                                  <serviceId>3 </serviceId>
                                  <arrTime>139.9846432544528</arrTime>
                                  <endTime>149.9846432544528</endTime>
                             </act>
                             <end>259.9668316441239</end>
                        </route>
                   </routes>
              </solution>
</problem>

for (int temp = 0; temp < nList.getLength(); temp++) {
    Node nNode = nList.item(temp);
    System.out.println("\nCurrent Element :" + nNode.getNodeName());
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) nNode;
        //System.out.println("route : "
                          // + eElement.getAttribute("id"));
        System.out.println("DriverId : "
                           + eElement.getElementsByTagName("driverId")
                             .item(0).getTextContent());
        System.out.println("vehicleId : "
                           + eElement.getElementsByTagName("vehicleId")
                             .item(0).getTextContent());

                NodeList optionList = eElement.getElementsByTagName("act");
    for (int j = 0; j < optionList.getLength(); ++j)
    {
        Element option = (Element) optionList.item(j);
        for(int k =0;k<1;++k){
        String optionText = option.getTextContent();
       address.add(optionText.replaceAll("[^A-Za-z]"," "));
        System.out.println("Citizen :"+optionText.replaceAll("[^A-Za-z]"," "));}
        ;


    }
             System.out.println("cost : "
                           + eElement.getElementsByTagName("end")
                             .item(0).getTextContent());
    }
}
} catch (Exception e) {
e.printStackTrace();
}

***更新***在这里我设法得到我想要的,但有一个概率,对于市民我只有第一个“行为”标签,我需要得到所有的标签价值

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author HP
 */
public class JavaApplication39 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            int totalVehicle;
            totalVehicle = 2;
            File fXmlFile = new File("C:/Users/HP/Desktop/solution.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
            Double requiredCost = 505.9208295302417;
            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
            // NodeList nList = doc.getElementsByTagName("route");
            System.out.println("----------------------------");
            NodeList nodeList = doc.getElementsByTagName("solution");
            for (int i = 0; i < nodeList.getLength(); i++) {

                Node solutionNode = nodeList.item(i);

                if (solutionNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element solutionElement = (Element) solutionNode;
                    Node costNode = solutionElement.getElementsByTagName("cost").item(0);
                    Node route = solutionElement.getElementsByTagName("routes").item(0);
                    // if correct cost, proceed to parse further
                    Double costValue = Double.valueOf(costNode.getTextContent());
                    if (Double.compare(requiredCost, costValue) == 0) {
                        System.out.println("working");
                        // there you go, found the node with the cost 505.9208295302417
                        // now just parse all the node elements you need here

                        System.out.println("cost : "
                                + solutionElement.getElementsByTagName("cost")
                                        .item(0).getTextContent());
                        for (int h = 0; h < totalVehicle; h++) {
                            System.out.println("DriverId : "
                                    + solutionElement.getElementsByTagName("driverId")
                                            .item(h).getTextContent().toString());
                            System.out.println("vehicleId : "
                                    + solutionElement.getElementsByTagName("vehicleId")
                                            .item(h).getTextContent());

                            System.out.println("citizen: "
                                    + solutionElement.getElementsByTagName("act")
                                            .item(h).getTextContent());


                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //System.out.println(address.get(1)); 
    }

}

1 个答案:

答案 0 :(得分:0)

只是我或我在代码中没有看到任何比较?检查节点是否有您需要的费用?您可以循环遍历解决方案节点(正如您所做的那样),一旦找到正确的成本值,就可以解析该节点所需的所有元素。

示例代码:

NodeList nodeList = doc.getElementsByTagName("solution");
Double requiredCost = 505.9208295302417;
for (int i = 0; i < nodeList.getLength(); i++) {

    Node solutionNode = nodeList.item(i);

    if (solutionNode.getNodeType() == Node.ELEMENT_NODE) {
        Element solutionElement = (Element) solutionNode;
        Node costNode = solutionElement.getElementsByTagName("cost").item(0);

        // if correct cost, proceed to parse further
        Double costValue = Double.valueOf(costNode.getTextContent());
        if (Double.compare(requiredCost, costValue) == 0) {

            // there you go, found the node with the cost 505.9208295302417
            // now just parse all the node elements you need here
        }
    }
}