This post helps you understand how to use the <x:parse> tag in the JSTL XML tags library with code example.

You know, the <x:parse> tag is used to parse XML content given in its source attribute or in its tag body.

 

JSTL <x:parse> Syntax:

<x:parse

  var="<string>"

  varDom="<string>"

  scope="<string>"

  scopeDom="<string>"

  doc="<string>"

  systemId="<string>"

  filter="<string>"/>

 

Attributes:

Name

Required

Description

var

False

Name of the scoped variable for the parsed XML document. The type of this variable is implementation dependent.

varDom

False

Name of the scoped variable for the parsed XML document. The type of this variable is org.w3c.dom.Document.

scope

False

Scope to store the var.

scopeDom

False

Scope to store the varDom.

doc

False

XML document to be parsed.

systemId

False

The URI of the XML document being parsed.

filter

False

The filter to be applied to the source document.

 

JSTL <x:parse> Example:

The following JSP code snippet parses an XML document and displays node located at position [1].

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd">
<html> 
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>&lt;x:parse&gt; Demo</title>
  </head>
  <body>
    <h1>&lt;x:parse&gt; Demo</h1>
    <c:import 
      url="http://localhost:8080/JSTL-Tag-Reference/tag-types/xml/citizens.xml" 
      var="citizenXML"/>
      <x:parse var="doc" xml="${citizenXML}" />
      <x:out select="$doc/citizens/citizen[1]/ssn" />            
      <x:out select="$doc/citizens/citizen[1]/firstname" />
      <x:out select="$doc/citizens/citizen[1]/lastname" />
   </body>
</html>

 

XML Document (given for reference)

<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<citizens>
    <citizen>
        <ssn>Z345T</ssn>
        <firstname>Cheryl</firstname>
        <lastname>Johnson</lastname>
        <role>Manager</role>
        <salary>12000</salary>
    </citizen>
    <citizen>
        <ssn>Z446T</ssn>
        <firstname>John</firstname>
        <lastname>Smith</lastname>
        <role>Employee</role>
        <salary>1000</salary>
    </citizen>
    <citizen>
        <ssn>Z335T</ssn>
        <firstname>Justin</firstname>
        <lastname>Claire</lastname>
        <role>Senior Manager</role>
        <salary>14000</salary>
    </citizen>
    <citizen>
        <ssn>Z389T</ssn>
        <firstname>Clark</firstname>
        <lastname>Rick</lastname>
        <role>Employee</role>
        <salary>2000</salary>
    </citizen>
</citizens>

 

Output:

x-parse

 

Recommended Usage of <x:parse> tag:

The <x:parse> tag is used to parse XML content from its source attribute or from its tag body.

 

Other JSTL XML Tags:

forEach  |  if  |  out  |  param  |  set  |  transform  |  choose, when, otherwise


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment