0

I need to check a bunch of .xml files for a specific permission, which is an attribute. If there is such a permission attribute i have to find out which value the attribute has.

this is my code which produces NullPointerExceptions:

public static void checkXmlPermissions(String path)
    {
        FileInputStream file;
        try                                                                                                 
        {
            file = new FileInputStream(new File(path));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 
                                                                                                            //XPath initialisieren;
            DocumentBuilder builder =  builderFactory.newDocumentBuilder();                          

            Document xmlDocument = builder.parse(file);

            XPath xPath =  XPathFactory.newInstance().newXPath();


            String expression ="//*[@permission]";                                                                          //Expression;
            NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);

            for (int i = 0; i < nodeList.getLength(); i++) 
            {
                String match = nodeList.item(i).getFirstChild().getNodeValue();
                System.out.println(match);

            }
        }
        catch(Exception ex)
        {
            System.out.println(ex);
        }

    }

I guess my mistake is the NodeList but I can´t find a solution by my own.

1
  • 2
    what line causes the exception? can you show example input? Commented Jul 2, 2015 at 7:10

1 Answer 1

1

change:

String match = nodeList.item(i).getFirstChild().getNodeValue();

to:

String match = nodeList.item(i).getTextContent();
Sign up to request clarification or add additional context in comments.

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.