1

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

    @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)
4

2 回答 2

1

方法名称:DocumentBuilder.parse(String Uri)将字符串 URI 作为参数,但not Array List of String Uri's

请根据您的需要做出决定。

  1. 文档 doc = dBuilder.parse(fXmlFile.get( 0));
  2. 文档 doc = dBuilder.parse(fXmlFile.get( 1));
于 2016-12-07T12:15:25.343 回答
1
dBuilder.parse(fXmlFile);

行不通 - 它需要一个字符串 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() + "]");    
                }    
            }
        }
    }
于 2016-12-07T12:17:40.847 回答