gpt4 book ai didi

java:找不到适合解析的方法(java.util.List)

转载 作者:行者123 更新时间:2023-11-30 02:43:16 25 4
gpt4 key购买 nike

我的代码有问题,我假设我缺少一个方法,我想创建一个字符串列表,然后打印出内容。现在我已经编写了一些代码,可以对一个字符串执行此操作,但我想对多个字符串执行此操作。

    @Test
public void xmlFileParse () throws ParserConfigurationException, IOException, SAXException {
List<String> fXmlFile = new ArrayList<String>();
fXmlFile.add("src/test/resources/fixtures/event.xml");
fXmlFile.add("src/test/resources/fixtures/country.xml");


DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
NodeList nodes = doc.getElementsByTagName("subscription-update").item(0).getChildNodes();

for(int i = 0; i < nodes.getLength(); i++){
Node node = nodes.item(i);
if(node.getNodeType() == ELEMENT_NODE){
System.out.println("TABLE: [" + node.getNodeName() + "] ID: [" + node.getAttributes().getNamedItem("id").getNodeValue() + "]");
}
}
}

这是我当前收到的错误。

Error:(65, 32) java: no suitable method found for parse(java.util.List<java.lang.String>)
method javax.xml.parsers.DocumentBuilder.parse(java.io.InputStream) is not applicable
(argument mismatch; java.util.List<java.lang.String> cannot be converted to java.io.InputStream)
method javax.xml.parsers.DocumentBuilder.parse(java.lang.String) is not applicable
(argument mismatch; java.util.List<java.lang.String> cannot be converted to java.lang.String)
method javax.xml.parsers.DocumentBuilder.parse(java.io.File) is not applicable
(argument mismatch; java.util.List<java.lang.String> cannot be converted to java.io.File)
method javax.xml.parsers.DocumentBuilder.parse(org.xml.sax.InputSource) is not applicable
(argument mismatch; java.util.List<java.lang.String> cannot be converted to org.xml.sax.InputSource)

最佳答案

dBuilder.parse(fXmlFile);

不起作用 - 它需要一个字符串 uri,而不是它们的列表。来自文档:

parse(String uri) Parse the content of the given URI as an XML document and return a new DOM Document object.

我认为你的代码应该如下所示:

@Test
public void xmlFileParse () throws ParserConfigurationException, IOException, SAXException {
List<String> fXmlFile = new ArrayList<String>();
fXmlFile.add("src/test/resources/fixtures/event.xml");
fXmlFile.add("src/test/resources/fixtures/country.xml");


DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

for(String uri: fXmlFile) {
Document doc = dBuilder.parse(uri);
NodeList nodes = doc.getElementsByTagName("subscription-update").item(0).getChildNodes();

for(int i = 0; i < nodes.getLength(); i++){
Node node = nodes.item(i);
if(node.getNodeType() == ELEMENT_NODE){
System.out.println("TABLE: [" + node.getNodeName() + "] ID: [" + node.getAttributes().getNamedItem("id").getNodeValue() + "]");
}
}
}
}

关于java:找不到适合解析的方法(java.util.List<java.lang.String>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41017070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com