Getting all the tags in an XML file using java
Asked Answered
P

2

6

Hi I want to get a list of all tags in an XML, and if some tags carry particular attribute I also want the value of the attribute. For example here are one specific examples,

<?xml version="1.0" encoding="utf-8"?>
<bbc.mobile.news.view.AVGalleryView android:background="@drawable/gallery_item_selector" android:padding="2.0dip" android:focusable="true" android:layout_width="139.0dip" android:layout_height="130.0dip"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <bbc.mobile.news.view.NewsImageView android:id="@id/galleryItemView" android:background="#00000000" android:padding="0.0dip" android:layout_width="@dimen/thumbnail_width" android:layout_height="@dimen/thumbnail_height" />
    <TextView android:textSize="13.0sp" android:textColor="@color/thumbnail_text" android:ellipsize="end" android:id="@id/articleTitleId" android:background="@color/thumbnail_text_bg" android:paddingLeft="5.0dip" android:paddingTop="2.0dip" android:paddingBottom="5.0dip" android:layout_width="139.0dip" android:layout_height="50.0dip" android:maxLines="2" android:layout_below="@id/galleryItemView" />
    <ImageView android:layout_gravity="center_vertical" android:id="@id/avIconView" android:background="#99000000" android:duplicateParentState="true" android:layout_width="40.0dip" android:layout_height="40.0dip" android:src="@drawable/icon_playvideo_selected" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" />
</bbc.mobile.news.view.AVGalleryView>

I am not interested about parent child relationship, I want to iterate to the deepest child if a parent-child relationship exists. and I also want the android:id and android:name attribute value if exists in a particular element.

problem is that you cannot know how deep a parent-child relation can be and where in the xml it will be. And you also dont know the tag names before. I can think of using recursion in my code , but I believe there is a simpler solution

Phelps answered 11/10, 2012 at 3:0 Comment(3)
I'd start with some research, How to read XML file in Java – (DOM Parser) is a nice simple exampleHeadline
Hi I tried that, but problem is the XML there is so simple, you cannot know how deep a parent-child relation can be and you also dont know the tag names before. I can think of using recursion, but I believe there is a simpler solution.Phelps
yes, I found the solution. Sharing it.Phelps
P
16

I found the solution, it was very simple, did not know before that getElementsByTagName("*") does that, here is my code,

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();
    System.out.println("Root element " + doc.getDocumentElement().getNodeName());

    NodeList nodeList=doc.getElementsByTagName("*");
    for (int i=0; i<nodeList.getLength(); i++) 
    {
        // Get element
        Element element = (Element)nodeList.item(i);
        System.out.println(element.getNodeName());
    }

I have found the solution here

Phelps answered 11/10, 2012 at 3:21 Comment(2)
is ter any implementation in JAXB?Blatman
how to print the values of the tag names if they exist?Masera
G
1

P basak's answer is perfect. I have used below code with the referenced url by P basak.

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class XMLToTags {

    public static void main(String[] args) {

        try {
            BufferedReader bf = new BufferedReader(
              new InputStreamReader(System.in));
            System.out.print("Enter XML File name: ");
            String xmlFile = bf.readLine();
            File file = new File(xmlFile);
            if(file.exists()){

                DocumentBuilderFactory factory = 
                  DocumentBuilderFactory.newInstance();

                DocumentBuilder builder = factory.newDocumentBuilder();
                org.w3c.dom.Document doc = builder.parse(xmlFile);

                NodeList list = doc.getElementsByTagName("*");
                System.out.println("XML Elements: ");
                for (int i=0; i<list.getLength(); i++) {

                    Element element = (Element)list.item(i);
                    System.out.println(element.getNodeName());
                }
            }
            else{
                    System.out.print("File not found!");
                }
            }
        catch (Exception e) {
            System.exit(1);
        }
    }    
}
Galenism answered 4/2, 2017 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.