1

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)
lospejos
  • 1,976
  • 3
  • 19
  • 35
Peter
  • 231
  • 1
  • 7
  • 22
  • show us your method call – 0x6C38 Dec 07 '16 at 12:12
  • You try to pass a List to a parse() method, but it supports only File, String, InputSource, InputStream. You have to iterate on your list and parse each list item (filename). – lospejos Dec 07 '16 at 12:14
  • Possible duplicate of [Parse XML string and build a list of strings](http://stackoverflow.com/questions/37109094/parse-xml-string-and-build-a-list-of-strings) – AxelH Dec 07 '16 at 12:15

2 Answers2

1

Method Name : DocumentBuilder.parse(String Uri) takes String URI as Argument but not Array List of String Uri's

Please take a decision based on your needs.

  1. Document doc = dBuilder.parse(fXmlFile.get(0));
  2. Document doc = dBuilder.parse(fXmlFile.get(1));
Mouli
  • 299
  • 3
  • 12
1
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() + "]");    
                }    
            }
        }
    }
Piotr Sołtysiak
  • 998
  • 5
  • 13