Java XML parsing with DocumentBuilderFactory - null nodes?

I have Java code to parse an XML file. However, my code returns null for my nodes.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse( new File( path ) );
rootElement = doc.getDocumentElement();
String str = rootElement.getLocalName();

When I type str, I get null. The path to the XML file is correct. Any ideas what could be the problem?

+3
source share
1 answer

Do you want a tag name? UserootElement.getTagName();

From Java docs:

public String getLocalName ()

Returns the local part of the qualified name of this node. For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE, and nodes created using the DOM Level 1 method, such as Document.createElement (), this is always null. C: DOM Level 2.

public String getTagName ()

. node.localName , . , :

 <elementExample id="demo"> ...
 </elementExample> , 

tagName "elementExample". , XML, DOM. HTML DOM tagName HTML , HTML-.

+3

All Articles