How to parse an XML in JSP?

JSPJava 8Object Oriented ProgrammingProgramming

The <x:parse> tag is used to parse the XML data specified either via an attribute or in the tag body.


The <x:parse> tag has the following attributes −

varA variable that contains the parsed XML dataNoNone
xmlText of the document to parse (String or Reader)NoBody
systemIdThe system identifier URI for parsing the documentNoNone
filterThe filter to be applied to the source documentNoNone
docXML document to be parsedNoPage
scopeScope of the variable specified in the var attributeNoPage
varDomA variable that contains the parsed XML dataNoPage
scopeDomScope of the variable specified in the varDom attributeNoPage


The following example shows how parse can be used to read the external XML file −

We have seen how we can parse XML from the body of the given document. Let us now put following content in the books.xml file −

      <name>Padam History</name>
      <name>Great Mistry</name>

Now try the following main.jsp, keeping in the same directory −

<%@ taglib prefix = "c" uri = "" %>
<%@ taglib prefix = "x" uri = "" %>
      <title>JSTL x:parse Tags</title>
      <h3>Books Info:</h3>
      <c:import var = "bookInfo" url = "http://localhost:8080/books.xml"/>
      <x:parse xml = "${bookInfo}" var = "output"/>
      <b>The title of the first book is</b>:
      <x:out select = "$output/books/book[1]/name" />
      <b>The price of the second book</b>:
      <x:out select = "$output/books/book[2]/price" />

Access the above JSP using http://localhost:8080/main.jsp, the following result will be displayed −

Books Info:
The title of the first book is:Padam History
The price of the second book: 2000
Updated on 30-Jul-2019 22:30:25