ccot
ccot

Reputation: 1873

read xml from jsp

lets say i have an xml file "path.xml" that goes like this:

<paths>
   <path id="first">
      <url>http://blablabla</url>
   </path>
   <path id="second">
      <url>http://blablabla</url>
   </path>
</paths>

i have a jsp file from which i want to read url based on the id. for example: i want to write in the jsp file some java code like:String path = get from xml file the "url", where path id = "second".

how is this done in jsp? i am not very experienced with Dom parsing in jsp

thanks a lot:)

Upvotes: 1

Views: 5397

Answers (2)

user357812
user357812

Reputation:

You could use XML Tag Library :

<x:parse doc="path.xml" var="doc" scope="application"/>
<x:out select="$doc/paths/path[id='second']/url"/>

Upvotes: 2

limc
limc

Reputation: 40176

First off, you shouldn't be writing scriptlets in your JSP to perform a processing like this. That should reside in your Java file, not JSP file.

To parse XML with Java, there are tons of examples you will find by just Googling around. Here's one to get you started: http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html

Upvotes: 1

Related Questions