Link to home
Start Free TrialLog in
Avatar of arichexe
arichexe

asked on

reading an xml file

How would I read the below MyApp.xml file, and display all the names on a web page?

<?xml version="1.0" ?>
<MyApp>
  <Names>
    <Name>John Doe</Name>
    <Name>Paul Smith</Name>
    <Name>Jane Doe</Name>
    <Name>Mary Smith</Name>
  </Names>
</MyApp>
Avatar of boonleng
boonleng
Flag of Malaysia image

The are several ways to read the XML file, you can use the JSTL tag such as the following example in th jsp:

<%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>
<%@ taglib uri='http://java.sun.com/jstl/xml'  prefix='x' %>

<%-- Import XML file --%>
<c:import var='myapp_xml' url='MyApp.xml'/>

<%-- Parse XML file --%>
<x:parse var='document' xml='${myapp_xml}'/>

<x:forEach select='$document//Names'>
  <x:out select='Name'/>
</x:forEach>


or use dom4j (http://www.dom4j.org/) or Xerces (http://xml.apache.org/xerces-j/) to parse the xml file, or use XSLT to read and format the xml to html.

Regards,
Boon Leng
Avatar of arichexe
arichexe

ASKER

I'm getting the error: "org.apache.jasper.JasperException: MyApp.jsp(8,0) According to TLD or attribute directive in tag file, attribute xml does not accept any expressions."  What could be causing this?
I got it to work, somewhat, but had to change the tag lib URL; however, all that's displaying is the first name, "John Doe."  Is there something wrong with the forEach logic?

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<%-- Import XML file --%>
<c:import var='myapp_xml' url='MyApp.xml'/>

<%-- Parse XML file --%>
<x:parse var='document' xml='${myapp_xml}'/>

<x:forEach select='$document//Names'>
  <x:out select='Name'/>
</x:forEach>
Sorry for the late reply, my line down for the entire day :(
And sorry for some of the mistakes on the code posted previously, I wrote it without testing it :p
The following should be working, let me know if you still have problem with it :)

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<%-- Import XML file --%>
<c:import var='myapp_xml' url='MyApp.xml'/>

<%-- Parse XML file --%>
<x:parse var='document' xml='${myapp_xml}'/>

<x:forEach select='$document//Name'>
  <x:out select='text()'/>
</x:forEach>
Works now, thanks; however, say I wanted save the names to a variable, so I could do some scripting, i.e.,
<% if (Name == 'John Doe') {some operation} %>.
You can try the following codes:

<x:forEach select='$document//Name'>
  <x:if select='text() = "John Doe"'>
        <c:set var="name"><x:out select='text()'/></c:set>
  </x:if>
</x:forEach>
<c:out value="${name}"/>
Works great, but what if I want to do more than just display the ${name} var; like get a substring of it via a JSP script?
ASKER CERTIFIED SOLUTION
Avatar of boonleng
boonleng
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works great!  Thanks for your help.