我的代码有问题,我想我缺少一种方法,我想创建一个字符串列表,然后打印出内容。现在,我编写了一些针对一个字符串执行此操作的代码,但我想针对多个字符串执行此操作。
@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);
将不起作用-它期望一个String uri,而不是它们的列表。从文档:
parse(String uri)将给定URI的内容解析为XML文档,并返回一个新的DOM Document对象。
我认为您的代码应如下所示:
@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() + "]");
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句