-1

I am trying to parse the XML file to get the attributes of a particular tag. Below is my XML file-

<?xml version="1.0" encoding="UTF-8"?>
<app hash='nv' name='Tech' package='1.0' version='13' filesize='200' create_date='01-03-1987' upate_date='07-09-2013' >
    <url>
        <name>RJ</name>
        <score>10</score>
    </url>
    <url>
        <name>ABC</name>
        <score>20</score>
    </url>
</app>

I am trying to get hash, name, package, version, filesize, create_date, update_date value from the above XML file.

Below is my code in which I am trying to parse the above XML file but I am not sure how to get the above required attributes from the app tag?

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

try {

    InputStream is = request.getInputStream();
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
    doc.getDocumentElement().normalize();

    System.out.println("root of xml file" + doc.getDocumentElement().getNodeName());

    //get hash, name, package value from the app tag in the XML file

    } catch (Exception e) {
      System.out.println(e);
    }

Can anyone please help me with this?

AKIWEB
  • 19,008
  • 67
  • 180
  • 294
  • Your question suggests that you've yet to look at a tutorial on the subject as you show no code that tries to extract information from the xml. That's where I'd start if this were me. Get some basic knowledge on the subject before coming here, since at the least it will help you to ask better questions. As an aside, you should never have an empty catch block for that way leads to madness. – Hovercraft Full Of Eels Jul 10 '13 at 02:52
  • I have already taken a look at the tutorial but somehow I am not able to understand how to get those. And also I was not able to find any similar tutorial that helps me understand accordingly. – AKIWEB Jul 10 '13 at 02:53
  • From your previous question it appears that you're just starting out with xml. You need to put in some study first. Really. There are no shortcuts, and in particular stackoverflow won't substitute for this. – Hovercraft Full Of Eels Jul 10 '13 at 02:54
  • A quick [Google](https://www.google.com.au/search?q=java%20xml%20attributes&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=np&source=hp) found [this](http://stackoverflow.com/questions/11240521/parsing-xml-attributes-in-java) and [this](http://stackoverflow.com/questions/11560173/how-can-i-read-xml-attributes-using-java) – MadProgrammer Jul 10 '13 at 03:03

1 Answers1

2

You can use Jsoup, it's a great library in Java for working with any XML-based document.

In your case, you can get the tag with the "app" tag and parse its attributes like so:

String result = getStringFromInputStream(is);

Document doc = Jsoup.parse(result, "", Parser.xmlParser());
Element e = doc.getElementsByTag("app").first();

System.out.println(e.attr("hash"));
System.out.println(e.attr("name"));

//etc...

In order to convert your InputStream into a String, you can use MyKong's method:

private static String getStringFromInputStream(InputStream is) {

    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();

    String line;
    try {

        br = new BufferedReader(new InputStreamReader(is));
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return sb.toString();

}

Good luck!

Ido Cohn
  • 1,685
  • 3
  • 21
  • 28