java: 找不到适合解析的方法(java.util.List<java.lang.String>)
java: no suitable method found for parse(java.util.List<java.lang.String>)
我的代码有问题我假设我缺少一个方法,我想创建一个字符串列表然后打印出内容。现在我已经编写了一些代码来为一个字符串执行此操作,但我想为多个字符串执行此操作。
@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)
方法名称:DocumentBuilder.parse(String Uri)
字符串 URI 作为参数但 not Array List of String Uri's
请根据您的需求做出决定。
- 文档文档 = dBuilder.parse(fXmlFile.get(
0
));
- 文档文档 = dBuilder.parse(fXmlFile.get(
1
));
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() + "]");
}
}
}
}
我的代码有问题我假设我缺少一个方法,我想创建一个字符串列表然后打印出内容。现在我已经编写了一些代码来为一个字符串执行此操作,但我想为多个字符串执行此操作。
@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)
方法名称:DocumentBuilder.parse(String Uri)
字符串 URI 作为参数但 not Array List of String Uri's
请根据您的需求做出决定。
- 文档文档 = dBuilder.parse(fXmlFile.get(
0
)); - 文档文档 = dBuilder.parse(fXmlFile.get(
1
));
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() + "]");
}
}
}
}