Search code examples
javaxmldomxml-parsingdomparser

How to read the particular XML tag from DOM parser in java <example mentioned>


The xml is -

<?xml version="1.0" encoding="UTF-8"?>
<app_content>
            <app_type value="Browser">
                  <item>
                        <item_name>sdk_version</item_name>
                        <item_value>01.00.001</item_value>
                        <item_save>01.00.001</item_save>
                        <item_desc>LG SDK Version</item_desc>
                        <item_type>edit</item_type>
                  </item>
                  <item>
                        <item_name>use_internet</item_name>
                        <item_value>Yes/No</item_value>
                        <item_save>Y/N</item_save>
                        <item_desc>Internet Use</item_desc>
                        <item_type>combo</item_type>
                  </item>
 </app_type>
</app_content>

I want to read the contents of each item tag, so I have 2 items tag inside app_type.

I made the code like this -

Element mainElement = (Element) document.getElementsByTagName("app_type").item(0);
NodeList browserCategories =  mainElement.getElementsByTagName("item"); //item
        for(int i=0;i<browserCategories.getLength();i++){           
        Element browserCategoryElement = (Element) browserCategories.item(i);
}

But after that I am not getting the attributes of each item and the value associated to each tags under item. Like I want to read all the values like sdk_version , 01.00.001 ......


Solution

  • Got the Solution-

    NodeList list = browserCategoryElement.getElementsByTagName("item_name");
    NamedNodeMap map = browserCategories.item(i).getAttributes();