我有一个XML文件,我需要在其中获取特定元素,我在编写逻辑以获取这些元素时遇到麻烦。在我的情况下,我希望得到第二个子元素中的所有值(父级) =解决方案是根标签)505.9208295302417有人告诉我使用比较if语句来获取元素
我的xmlfile
<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>
给出的逻辑
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
}
}
}
我试图做的是
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 routes = solutionElement.getElementsByTagName("routes").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
System.out.println("DriverId : "
+ solutionElement.getElementsByTagName("driverId")
.item(0).getTextContent());
System.out.println("vehicleId : "
+ solutionElement.getElementsByTagName("vehicleId")
.item(0).getTextContent());
NodeList optionList = solutionElement.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]", " "));
}
;
}
}
}
}
目前使用xpath
更新 package xpath;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
public class Xpath {
/**
* @param args the command line arguments
*/
public static void main(String[] args)throws Exception {
DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = Factory.newDocumentBuilder();
Document doc = builder.parse("C:/Users/HP/Desktop/solution.xml");
//creating an XPathFactory:
XPathFactory factory = XPathFactory.newInstance();
//using this factory to create an XPath object:
XPath xpath = factory.newXPath();
// XPath Query for showing all nodes value
XPathExpression expr = xpath.compile("problem/solutions/solution[cost='505.9208295302417']/routes/route");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println("test"+nodes.item(i).getTextContent());
Element el = (Element) nodes.item(i);
System.out.println("Element currently in: " + el.getNodeName());
// seach for the Text children
if (el.getFirstChild().getNodeType() == Node.TEXT_NODE)
System.out.println("driverid:" + el.getFirstChild().getTextContent());
Node vehicle = el.getFirstChild().getFirstChild();
NodeList children = el.getChildNodes();
}
}
}
答案 0 :(得分:0)
提示:您可以使用xpath直接访问具有所需值的// solution / cost的所有route
个后代,然后迭代这些节点以获取值。
使用xmllint实用程序进行演示
xmllint --xpath '//solution[cost = "505.9208295302417"]/routes/route' test.xml
<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>
<!-- more route tags here -->