如何在JSP中从这个XML中提取值 [英] How to extract value from this XML in JSP

查看:181
本文介绍了如何在JSP中从这个XML中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML如下(有100行):

I have an XML as below (has 100s of line):

    <root>

      <data v="1.0">

        <cellimage counter="0" cash_filename="C:\Temp\_TempFolder\39d437f08cc302876a70a0f91b137991_h.jpg" width="94" height="141" />

        <cellimage counter="1" cash_filename="C:\Temp\_TempFolder\39d437f08cc302876a70a0f91b137991_h.jpg" width="94" height="141" />

      </data>

    </root>

任何人都可以告诉我如何循环它并提取'counter'和'cash_filename等属性'来自JSP中的上述XML文件。

Can anyone please tell me how I can loop through it and extract attributes like 'counter' and 'cash_filename' from the above XML file in JSP.

到目前为止,我有以下代码:

So far I have following code:

    <%
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse("http://localhost:8080/data.xml");

        NodeList nl = doc.getElementsByTagName("cellimage");
            for (int i = 0; i < nl.getLength(); i++) {
                //Not sure what to do here!
            }
    %>


推荐答案

你可以很简单地获得你的物品:

you can get your items quite simple:

NodeList nl = doc.getElementsByTagName("cellimage");
    Element el;
    Integer counter;
    String fName;

    for (int i = 0; i < nl.getLength(); i++) {
        //Not sure what to do here!
        el = (org.w3c.dom.Element) nl.item(i);
        counter = Integer.valueOf(el.getAttribute("counter"));
        fName = el.getAttribute("cash_filename");
    }

这篇关于如何在JSP中从这个XML中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆