如何在JSP中解析/读取此XML文件

时间:2013-04-15 15:48:27

标签: java xml jsp xml-parsing

我有以下XML文件,我正在尝试使用http://www.easywayserver.com/blog/java-read-xml-file/上的教程从JSP中读取:

XML文件1:

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

XML文件2

<people>
  <person>
    <name>Joe</name>
    <age>30</age>
  </person>
  <person>
    <name>Rob</name>
    <age>29</age>
  </person>
</people>

上面的XML文件1在后续行引发错误,但第二个XML文件工作得很好。有人可以告诉我如何让XML文件1工作吗?

 String dateTagValue=dateElements.getChildNodes().item(0).getNodeValue();

错误堆栈跟踪:

Caused by: org.apache.jasper.JasperException: An exception occurred processing JSP page /html/archivedetails/view.jsp at line 33

30:     for (int i = 0; i < nl.getLength(); i++) {
31:         NodeList nameNlc = doc.getElementsByTagName("counter");
32:         Element nameElements = (Element) nameNlc.item(i);
33:         String nameTagValue = nameElements.getChildNodes().item(0).getNodeValue();
34: 
35:         out.println("name :" + nameTagValue + "<br>");
36:     }


Stacktrace:
    at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:73)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
    at com.liferay.portlet.PortletRequestDispatcherImpl.dispatch(PortletRequestDispatcherImpl.java:323)
    at com.liferay.portlet.PortletRequestDispatcherImpl.include(PortletRequestDispatcherImpl.java:105)
    ... 167 more
Caused by: java.lang.NullPointerException
    at org.apache.jsp.html.archivedetails.view_jsp._jspService(view_jsp.java:152)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    ... 182 more

1 个答案:

答案 0 :(得分:0)

例外情况在

String nameTagValue = nameElements.getChildNodes().item(0).getNodeValue();

因此nameElements为空或者没有子节点(getChildNodes()返回null)。

代码非常可疑,因为您循环遍历nl,但随后使用索引访问nameNlc的元素,这些元素可能具有相同数量的元素,也可能不具有相同数量的元素。此外,您正在寻找名为<counter>的元素,这些元素不会出现在任何一个样本中。

错误消息与您正在显示的内容之间存在严重脱节,我怀疑您需要仔细查看您正在做的事情。也可能存在其他不一致之处。

我意识到这是一个边缘作为答案(作为评论可能更好)但是评论太长了,并且鉴于你的帖子中的不一致,可能是你所希望的最好的。