从XML文件中获取特定标记的属性

时间:2022-11-27 17:05:39

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

我试图解析XML文件以获取特定标记的属性。以下是我的XML文件 -

<?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.

我试图从上面的XML文件中获取哈希,名称,包,版本,filesize,create_date,update_date值。

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?

下面是我的代码,我试图解析上面的XML文件,但我不知道如何从app标签获取上述必需属性?

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?

有人可以帮我这个吗?

1 个解决方案

#1


2  

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

你可以使用Jsoup,它是一个很棒的Java库,用于处理任何基于XML的文档。

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

在您的情况下,您可以使用“app”标记获取标记并解析其属性,如下所示:

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:

要将InputStream转换为String,可以使用MyKong的方法:

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!

#1


2  

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

你可以使用Jsoup,它是一个很棒的Java库,用于处理任何基于XML的文档。

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

在您的情况下,您可以使用“app”标记获取标记并解析其属性,如下所示:

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:

要将InputStream转换为String,可以使用MyKong的方法:

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!