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)
3
  • show us your method call Commented Dec 7, 2016 at 12:12
  • You try to pass a List<String> 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). Commented Dec 7, 2016 at 12:14
  • Possible duplicate of Parse XML string and build a list of strings Commented Dec 7, 2016 at 12:15

2 Answers 2

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() + "]");    
                }    
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.