我的代码出现问题我假设我错过了一个方法,我想创建一个字符串列表,然后打印出内容。现在我已经编写了一些代码来为一个字符串执行此操作,但我想为多个字符串执行此操作。
@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)
答案 0 :(得分:1)
方法名称:git pull origin master
需要
字符串URI作为参数但DocumentBuilder.parse(String Uri)
请根据您的需要做出决定。
not Array List of String Uri's
)); 0
)); 答案 1 :(得分:1)
dBuilder.parse(fXmlFile);
不起作用 - 它期望一个字符串uri,而不是它们的列表。来自docs:
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() + "]");
}
}
}
}