Calling method on Document interface in Java

2019-09-01 04:24发布

I am trying to parse an XML file in Java and after getting the DocumentBuilder object, I call the parse method on it to get a Document object. e.g. Document dom = docbuild.parse(fileName);

Then to get the root of the XML file, I use the method dom.getDocumentElement();. Since Document is an interface as defined in the javadocs, how are we able to call a method on it without defining it first?

My main objective is to create a class that inherits the Document interface, so I have to implement it. How do I go about doing this?

2条回答
仙女界的扛把子
2楼-- · 2019-09-01 05:07

The use of the document is as follow:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse("StringOfTheSource");

These tree steps will fix your problem.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-09-01 05:22

DocumentBuilder returns some implementation of Document. You don't have to worry about implementing the Document interface, somebody has already done that for you. The returned document will represent the XML document, which is what you want.

查看更多
登录 后发表回答