Java 将XML数据追加到ArrayList
我创建了一个XML解析器,将XML文件中的信息检索到java,然后尝试将这些数据存储到ArrayList中,以便将ArrayList用于我的方法。 当我打印它时,它似乎工作得很好。但是,由于某种原因,我得到了一个名为getAllRoutes的方法,它返回了错误的路由数Java 将XML数据追加到ArrayList,java,xml,arraylist,multidimensional-array,xml-parsing,Java,Xml,Arraylist,Multidimensional Array,Xml Parsing,我创建了一个XML解析器,将XML文件中的信息检索到java,然后尝试将这些数据存储到ArrayList中,以便将ArrayList用于我的方法。 当我打印它时,它似乎工作得很好。但是,由于某种原因,我得到了一个名为getAllRoutes的方法,它返回了错误的路由数 如果这对Khaled有帮助,请告诉我 试试这个: import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory;
如果这对Khaled有帮助,请告诉我 试试这个:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathExpressionException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.List;
import java.util.ArrayList;
class Main {
public static Iterable<Node> iterable(final NodeList nodeList) {
return () -> new Iterator<Node>() {
private int index = 0;
@Override
public boolean hasNext() {
return index < nodeList.getLength();
}
@Override
public Node next() {
if (!hasNext())
throw new NoSuchElementException();
return nodeList.item(index++);
}
};
}
private static List<String> evaluateXPath(Document document, String xpathExpression)
{
// Create XPathFactory object
XPathFactory xpathFactory = XPathFactory.newInstance();
// Create XPath object
XPath xpath = xpathFactory.newXPath();
List<String> values = new ArrayList<>();
try
{
// Create XPathExpression object
XPathExpression expr = xpath.compile(xpathExpression);
// Evaluate expression result on XML document
NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
values.add(nodes.item(i).getNodeValue());
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return values;
}
public static void main(String[] args) throws Exception {
//Build DOM
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("data.xml");
//Create XPath
XPathFactory xpathfactory = XPathFactory.newInstance();
XPath xpath = xpathfactory.newXPath();
String xpathRoot = "//Routes/Route";
XPathExpression expr = xpath.compile(xpathRoot);
int i = 0;
for (Node n : iterable((NodeList) expr.evaluate(doc, XPathConstants.NODESET))) {
i++;
String xpe2 = String.format("%s[%d]/%s/text()", xpathRoot, i, "FlightNumber");
System.out.println("FxPe: " + xpe2);
System.out.println("Flight Number: " + evaluateXPath(doc, xpe2).get(0));
for (Node n2 : iterable(n.getChildNodes())) {
System.out.println(n2.getTextContent());
}
}
}
}
import javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.xpath.xpath;
导入javax.xml.xpath.XPathConstants;
导入javax.xml.xpath.XPathExpression;
导入javax.xml.xpath.XPathFactory;
导入javax.xml.xpath.XPathExpressionException;
导入org.w3c.dom.Document;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
导入java.util.Iterator;
导入java.util.NoSuchElementException;
导入java.util.List;
导入java.util.ArrayList;
班长{
公共静态可编辑可编辑(最终节点列表节点列表){
return()->new Iterator(){
私有整数指数=0;
@凌驾
公共布尔hasNext(){
返回索引
看到它在行动吗
有用链接:
- 3:JavaXPath示例和示例
Route r=新路由()代码>应该在if语句中
if(c.getNodeName().equals(“路由”))
注释掉的add位于正确的位置-另一个位置错误。请移动
routes.add(r);
里面
if (c.getNodeName().equals("Route")) {
请举例说明如何使用方法getAllRoutes(),有一个标记负责运行和测试我的方法,并为每个方法提供反馈,即getAllRoutes()的反馈;is:测试失败,因为getAllRoutes()在加载引发此异常的数据@Daniyarthat java.lang.NullPointerException后返回了错误数量的路由。另外,请检查为什么元素有4个子元素如果获得NullPointerException,请分享,有一个标记测试我的代码,它说“测试失败,因为RouteDAO:java.lang.NullPointerException引发了异常”line引发NullPointerException的唯一方式是如果routes为null,但这里不是这样,当我们初始化声明本身时,routesI尝试包括路由r=newRoute();在if语句中,但这不起作用,现在它说在运行该方法时返回了错误的路由数,这意味着它实际上返回了一个数字,但由于某种原因它是错误的,而如果我取消注释注释了已注释的路由。add(r);出于某种原因,它会引发空指针异常即使在我尝试了您在上面建议的在if语句中包含新路由@JGFMKi尝试了您的XPath方法之后,它仍然可以正常工作,但是,您将其填充到我的ArrayList以便我可以将其用于getAllRoutes()的最佳方法是什么;方法?您可以在外部循环中创建一个新的路由对象,使用属性名称数组填充路由对象的所有属性,为每个属性构建xpath表达式。然后,将所有特性指定给管线对象后,将其添加到“管线”集合。我倾向于在每次处理XML时生成一个新的ArrayList。也许你的原始代码中也存在这个问题。在你原来的帖子中,你注释掉的add方法是正确的位置。