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>
arichexeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

boonlengCommented:
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
arichexeAuthor Commented:
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?
arichexeAuthor Commented:
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>
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

boonlengCommented:
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>
arichexeAuthor Commented:
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} %>.
boonlengCommented:
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}"/>
arichexeAuthor Commented:
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?
boonlengCommented:
Depends on what you want to do on the value,
You can either use XPath function (http://www.w3schools.com/xpath/xpath_functions.asp) to manipulate the value directly.
Example:
<x:out select='substring(text(), 1, 4)'/>

Or use scriptlet if you need more flexibilities.
Example:
...
<c:set var="name" scope="page"><x:out select='text()'/></c:set>
...
<%=pageContext.getAttribute("name").toString().substring(...)%>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arichexeAuthor Commented:
Works great!  Thanks for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.