0

I am looking to get only the tag name, and not it's children.

I have an xml like this:

  <RESPONSE>
    <RESULT>                                              !--TableName
       <ADDRESS1>123 Main Street</ADDRESS1>               !--ColumnName
       <ZIP>12345</ZIP>                                   !--ColumnName
    </RESULT>
    <RESULT>                                              !--TableName
      <ADDRESS1>245 Elm Street</ADDRESS1>                 !--ColumnName
      <ZIP>45678</ZIP>                                    !--ColumnName
   </RESULT>
   <VIN>                                                  !--TableName
      <VIN_NUM>1K45678RTW23</VIN>                         !--ColumnName
   </VIN>
   ….
</REPSONSE>

I am trying to dynamically save the xml into it's appropriate table and column names. So, I want to extract whatever the first element is, and assign it to a table name variable, and then it's children as columns.

Here is what I am doing so far:

    private void extractToTableSet(Document doc, int appseqno ) throws Exception
{
    NodeList responseList = doc.getElementsByTagName("RESPONSE");
    for (int i = 0; i < responseList.getLength(); i++) {
        Node currentNode = responseList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            Element tableElement = (Element) responseList.item(i);
            if (tableElement != null && tableElement.hasChildNodes()) {
                for (columnNode = tableElement.getFirstChild(); columnNode != null; columnNode = columnNode.getNextSibling()) {
                    if (columnNode.getNodeType() == Node.TEXT_NODE) {
                        columnName = columnNode.getNodeValue;
                    }
                }
            }
        }
    }
}

This way I am only able to get the values in the child nodes. Is there a way to get the name of the Element tags? Like I want to extract the value RESULT from the Document object.

3
  • 2
    check element.getTagName: w3.org/2003/01/dom2-javadoc/org/w3c/dom/… Commented Aug 29, 2014 at 19:37
  • are you trying to retrieve tagname? Commented Aug 29, 2014 at 19:39
  • yes, I am trying to retrieve tagname Commented Aug 29, 2014 at 19:47

2 Answers 2

1

In DOM, an element name is retrieved using Node.getNodeName().

Example:

if(node.getNodeType() == Node.ELEMENT_NODE) {
    String elementName = node.getNodeName();
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a difference with respect to getTagName?
No it's exactly the same for element nodes. getNodeName also applies for elements, attributes and other node types.
1

To get element's tagname :

Element tableElement = (Element) responseList.item(i);
    String tagname = tableElement .getTagName();

1 Comment

Could you please add some explanation, such as why this works, and what the OP did that was wrong?

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.