Search code examples
javaparsexml

java: no suitable method found for parse(java.util.List<java.lang.String>)


I'm having an issue with my code I'm assuming I'm missing a method, I want to create a list of strings and then print out the contents. Now I have written some code that does this for one string but I'd like to do it for a number of strings.

    @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() + "]");    
                }    
            }
        }

Here's the error I'm currently receiving.

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)

Solution

  • dBuilder.parse(fXmlFile);
    

    won't work - it expect one String uri, not list of them. From docs:

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

    I think your code should look like this:

    @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() + "]");    
                    }    
                }
            }
        }