通过jsp按顺序(按所需顺序)读取jsp中的xml数据

时间:2014-06-25 05:25:39

标签: xml jsp

`

    <microgame> 
    <gamename>Thunderstruck2</gamename>
    <gametype>Bonus Slot</gametype>
    <imagename>/images/microgame/thunderstruck2.png</imagename>
    <provider>Microgame</provider>
    <sequence>4</sequence>
    </microgame>    <microgame> 
    <gamename>TombRaider</gamename>
    <gametype>Bonus Slot</gametype>
    <imagename>/images/microgame/tomb-raider.png</imagename>
    <provider>Microgame</provider>
    <sequence>5</sequence>
    </microgame>    <microgame> 
    <gamename>Cashapillar</gamename>
    <gametype>Video Slot</gametype>
    <imagename>/images/microgame/cashapillar.png</imagename>
    <provider>Microgame</provider>
    <sequence>1</sequence>
    </microgame>

`

所以我的问题是,我想根据序列获取图像名称,例如,如果序列= 1,那么我应该得到/images/microgame/cashapillar.png,如果sequence = 5则/ images / microgame / tomb-raider.png等。

请允许任何人帮我解决这个问题,我一直在为此编写代码,但没有按顺序得到答案。如果有人能帮助我完成它,我将感激不尽。感谢

1 个答案:

答案 0 :(得分:0)

..在 imgname <显示 序列号 和相应image的表格/ p>

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();    
DocumentBuilder db = dbf.newDocumentBuilder();    
Document doc = db.parse("../.../name.xml");    
NodeList imgN = doc.getElementsByTagName("imagename");
NodeList seq = doc.getElementsByTagName("sequence");
%>    
<html>
    <head>
        <title>Imagename vs Sequence</title>
    </head>    
    <body>
        <table border="1">
            <%
        ArrayList<String[]> listX=new ArrayList<String[]>();
        for(int i=0;i<=seq.getLength()-1;i++)
        {
            listX.add(new String[]{imgN.item(i).getFirstChild().getNodeValue(), seq.item(i).getFirstChild().getNodeValue()});
        }

        Collections.sort(listX, new Comparator<String[]>() {
              public int compare(String[] first, String[] second) {
                    return (Integer.parseInt(first[1]) < Integer.parseInt(second[1]) ) ? -1: (Integer.parseInt(first[1]) > Integer.parseInt(second[1]) ) ? 1:0 ;
                  }
                });

            for(int i=0;i<=listX.size()-1;i++) {
                String[] s=listX.get(i);
        %>
                <tr>
                    <td>
                        <img src="<%= s[0] %>" />
                    </td>
                    <td>
                        <%= s[1]%>
                    </td>
                </tr>
                <%
        }
        %>
        </table>
    </body>
</html>