Skip to Content

DOM Parser method

Hi SDNites,

I am trying to implement a JAVA mapping using DOM parser and have a query around using methods getElementsByTagName. Below is the code snippet,

dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nodeList = doc.getElementsByTagName("Employee");


As getElementsByTagName is method of interface Element while in the above Document class instance doc is trying to access the method and is working. Can you please let me know why it is allowed to access this method from a Document class which does not have this method.

Regards,

Abhi

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Apr 12, 2017 at 05:32 PM

    Hi Abhi!

    First, Document is an interface too, not a class. It extends Node interface.

    Second, why not to have methods with the same name for different interfaces?

    Interface Document has its own getElementByTagName() method.

    Regards, Evgeniy.

    Add comment
    10|10000 characters needed characters exceeded