1

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

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

谁能告诉我如何循环遍历它并从 JSP 中的上述 XML 文件中提取“counter”和“cash_filename”等属性。

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

    <%
        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!
            }
    %>
4

1 回答 1

3

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

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");
    }
于 2013-04-16T10:30:19.520 回答