How to use <x:parse> tag of JSTL for parsing XML documents


If you want to parse a XML document, you can use x:parse tag like the following example;

Before using this example don’t forget to include standard.jar and jstl.jar inside lib folder.

Here is the content for the XML file used in this example

<?xml version="1.0" encoding="UTF-8"?>
<SERVERCONFIG>
<SERVERS>
<SERVER NAME="server1" ENABLED="Y">
<IP>10.1.2.1</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>12345</PASSWORD>
<DESCRIPTION>Application Server1</DESCRIPTION>
</SERVER>
<SERVER NAME="server2" ENABLED="Y">
<IP>10.1.2.2</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>54321</PASSWORD>
<DESCRIPTION>Application Server1</DESCRIPTION>
</SERVER>
<SERVER NAME="server3" ENABLED="N">
<IP>10.1.2.3</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>012345</PASSWORD>
<DESCRIPTION>Application Server3</DESCRIPTION>
</SERVER>
<SERVER NAME="server4" ENABLED="Y">
<IP>10.1.2.4</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>0123456</PASSWORD>
<DESCRIPTION>Application Server4</DESCRIPTION>
</SERVER>
<SERVER NAME="server5" ENABLED="N">
<IP>10.1.2.5</IP>
<USERNAME>admin</USERNAME>
<PASSWORD>01234567</PASSWORD>
<DESCRIPTION>Application Server5</DESCRIPTION>
</SERVER>
</SERVERS>
</SERVERCONFIG>

And the JSP page for processing XML content

index.jsp file

<%--
Document   : index
Created on : 16.Eyl.2011, 11:35:25
Author     : tunatore
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body style="background-color: aliceblue">
<center>
<h1>&lt;x:parse&gt; example</h1>
<!--c:import var="server" url="http://localhost:8080/JSTLxparseTagExample/servers.xml"/>-->
<c:import var="server" url="servers.xml"/>
<x:parse xml="${server}" var="output"/>
<table>
<tr>
<td align="center"><b> Getting the first server's information using XPath evaluation of &lt;x:out&gt; tag </b></td>
</tr>
<tr>
<td align="center">
<table border="2">
<tr><td><b>IP:</b></td><td><b><x:out select="$output/SERVERCONFIG/SERVERS/SERVER[1]/IP" /></b></td></tr>
<tr><td><b>USERNAME:</b></td><td><b><x:out select="$output/SERVERCONFIG/SERVERS/SERVER[1]/USERNAME" /></b></td></tr>
<tr><td><b>PASSWORD:</b></td><td><b><x:out select="$output/SERVERCONFIG/SERVERS/SERVER[1]/PASSWORD" /></b></td></tr>
<tr><td><b>DESCRIPTION:</b></td><td><b><x:out select="$output/SERVERCONFIG/SERVERS/SERVER[1]/DESCRIPTION" /></b></td></tr>
</table>
</td>
</tr>
<tr>
<td align="center"><b> Getting all servers using &lt;x:forEach&gt; tag  </b></td>
</tr>
<tr>
<td align="center">
<table border="2">
<x:forEach select="$output/SERVERCONFIG/SERVERS/SERVER"  >
<tr><td><b>SERVER--></b></td><td><b><x:out select="@NAME" /></b></td></tr>
<tr><td><b>IP:</b></td><td><b><x:out select="IP" /></b></td></tr>
<tr><td><b>USERNAME:</b></td><td><b><x:out select="USERNAME" /></b></td></tr>
<tr><td><b>PASSWORD:</b></td><td><b><x:out select="PASSWORD" /></b></td></tr>
<tr><td><b>DESCRIPTION:</b></td><td><b><x:out select="DESCRIPTION" /></b></td></tr>
</x:forEach>
</table>
</td>
</tr>
</table>
</center>
</body>
</html>

And this is the output for the program;

Download sample project

How to use EXTRACT(XML) function in ORACLE DB


Friends the following udemy course created by me;

      Click here to attend Spring Framework 4.x and certification course with a discount

Spring Framework and Core Spring Certification Udemy course with discount
Spring Framework and Core Spring Certification Udemy course with a discount coupon

Extract(XML) function is used to get a node or nodes inside a XML document stored in Oracle DB. The syntax for EXTRACT function is;

EXTRACT(XML-Document-Column, ‘XPath expression‘)

EXTRACT(XML-Document-Column, ‘XPath expression’, ‘namespace’)

Example usages;

CREATE TABLE LIBRARY
(
ID_COLUMN NUMBER PRIMARY KEY,
XML_DATA_COLUMN XMLType
);

INSERT INTO LIBRARY(ID_COLUMN ,XML_DATA_COLUMN) VALUES
( 1
, XMLType(‘<?xml version=”1.0″ encoding=”UTF-8″?>
<LIBRARY>
    <BOOKS>
        <BOOK isbn=”ABCD7327923″>
            <NAME>Java Programing</NAME>
            <SUBJECT>Java J2EE</SUBJECT>
            <AUTHORS>
                <AUTHOR>Tuna TORE</AUTHOR>
                <AUTHOR>Linus Torvalds</AUTHOR>
                <AUTHOR>James Gosling</AUTHOR>
            </AUTHORS>
        </BOOK>
        <BOOK isbn=”DFGH09093232″>
            <NAME>XPATH for Dummies</NAME>
            <SUBJECT>XPATH development</SUBJECT>
            <AUTHORS>
                <AUTHOR>Linus Torvalds</AUTHOR>
                <AUTHOR>John Hawking</AUTHOR>
            </AUTHORS>
        </BOOK>
        <BOOK isbn=”DSKL2393A”>
            <NAME>J2EE Patterns</NAME>
            <SUBJECT>Design Patterns</SUBJECT>
            <AUTHORS>
                <AUTHOR>Aka Tuna</AUTHOR>
            </AUTHORS>
        </BOOK>
    </BOOKS>
    <DVDS>
        <DVD id=”123456″>
            <NAME>Music DVD</NAME>
            <CONTENT>Music</CONTENT>
            <AUTHORS>
                <AUTHOR>James Gosling</AUTHOR>
                <AUTHOR>Bill Gates</AUTHOR>
            </AUTHORS>
        </DVD>
        <DVD id=”3213324″>
            <NAME>Natural Science</NAME>
            <CONTENT>Science</CONTENT>
            <AUTHORS>
                <AUTHOR>John Green</AUTHOR>
                <AUTHOR>Bill Gates</AUTHOR>
            </AUTHORS>
        </DVD>
        <DVD id=”4353534″>
            <NAME>Rally</NAME>
            <CONTENT>Race</CONTENT>
            <AUTHORS>
                <AUTHOR>Tuna</AUTHOR>
            </AUTHORS>
        </DVD>
    </DVDS>
</LIBRARY>
‘));

–you can get all DVDs in the library with the following query
SELECT ID_COLUMN, EXTRACT(XML_DATA_COLUMN, ‘/LIBRARY/DVDS/DVD‘)
FROM LIBRARY

–result will be

<DVD id=”123456″>
    <NAME>Music DVD</NAME>
    <CONTENT>Music</CONTENT>
    <AUTHORS>
        <AUTHOR>James Gosling</AUTHOR>
        <AUTHOR>Bill Gates</AUTHOR>
    </AUTHORS>
</DVD>
<DVD id=”3213324″>
    <NAME>Natural Science</NAME>
    <CONTENT>Science</CONTENT>
    <AUTHORS>
        <AUTHOR>John Green</AUTHOR>
        <AUTHOR>Bill Gates</AUTHOR>
    </AUTHORS>
</DVD>
<DVD id=”4353534″>
    <NAME>Rally</NAME>
    <CONTENT>Race</CONTENT>
    <AUTHORS>
        <AUTHOR>Tuna</AUTHOR>
    </AUTHORS>
</DVD>

–or you can get a specific DVD with the following query DVD having id –> 4353534
SELECT ID_COLUMN, EXTRACT(XML_DATA_COLUMN, ‘/LIBRARY/DVDS/DVD[@id=4353534]‘)
FROM LIBRARY

— the result is
<DVD id=”4353534″>
    <NAME>Rally</NAME>
    <CONTENT>Race</CONTENT>
    <AUTHORS>
        <AUTHOR>Tuna</AUTHOR>
    </AUTHORS>
</DVD>

You can also use the EXTRACTVALUE function for getting only the value inside XML tags
If you want to get value (name) for the DVD having id number –> 3213324

SELECT ID_COLUMN, EXTRACTVALUE(XML_DATA_COLUMN, ‘/LIBRARY/DVDS/DVD[@id=3213324]/NAME‘)
FROM LIBRARY

— the result is
Natural Science

And if you want to get XML tags for the above operation use EXTRACT instead of using EXTRACTVALUE

SELECT ID_COLUMN, EXTRACT(XML_DATA_COLUMN, ‘/LIBRARY/DVDS/DVD[@id=3213324]/NAME‘)
FROM LIBRARY

— the result is
<NAME>Natural Science</NAME>

You can also use EXTRACTVALUE in the WHERE clause of a SQL
like the following query

SELECT EXTRACT(XML_DATA_COLUMN, ‘/LIBRARY/DVDS/DVD[@id=3213324]‘) FROM LIBRARY
WHERE EXTRACTVALUE(XML_DATA_COLUMN, ‘/LIBRARY/DVDS/DVD[@id=3213324]/NAME‘) = ‘Natural Science’;

— the result is
<DVD id=”3213324″>
    <NAME>Natural Science</NAME>
    <CONTENT>Science</CONTENT>
    <AUTHORS>
        <AUTHOR>John Green</AUTHOR>
        <AUTHOR>Bill Gates</AUTHOR>
    </AUTHORS>
</DVD>


How to use XPATH in JAVA (simple example)


In this post, I will show you how to parse a XML document by using XPath technology on the Java Platform.

Here is the XML sample for using as a content for parsing operations;

books.xml file content;

<?xml version="1.0" encoding="UTF-8"?>
<LIBRARY>
    <BOOKS>
        <BOOK isbn="ABCD7327923">
            <NAME>Java Programing</NAME>
            <SUBJECT>Java J2EE</SUBJECT>
            <AUTHORS>
                <AUTHOR>Tuna TORE</AUTHOR>
                <AUTHOR>Linus Torvalds</AUTHOR>
                <AUTHOR>James Gosling</AUTHOR>
            </AUTHORS>
        </BOOK>
        <BOOK isbn="DFGH09093232">
             <NAME>XPATH for Dummies</NAME>
           <SUBJECT> XPATH development</SUBJECT>
            <AUTHORS>
                <AUTHOR>Linus Torvalds</AUTHOR>
                <AUTHOR>John Hawking</AUTHOR>
            </AUTHORS>
        </BOOK>
        <BOOK isbn="DSKL2393A">
            <NAME>J2EE Patterns</NAME>
            <SUBJECT>Design Patterns</SUBJECT>
            <AUTHORS>
                <AUTHOR>Aka Tuna</AUTHOR>
            </AUTHORS>
        </BOOK>
    </BOOKS>
    <DVDS>
        <DVD id="123456">
            <NAME>Music DVD</NAME>
            <CONTENT>Music</CONTENT>
            <AUTHORS>
                <AUTHOR>James Gosling</AUTHOR>
                <AUTHOR>Bill Gates</AUTHOR>
            </AUTHORS>
        </DVD>
        <DVD id="3213324">
            <NAME>Natural Science</NAME>
            <CONTENT>Science</CONTENT>
            <AUTHORS>
                <AUTHOR>John Green</AUTHOR>
                <AUTHOR>Bill Gates</AUTHOR>
            </AUTHORS>
        </DVD>
        <DVD id="4353534">
            <NAME>Rally</NAME>
            <CONTENT>Race</CONTENT>
            <AUTHORS>
                <AUTHOR>Tuna</AUTHOR>
            </AUTHORS>
        </DVD>
    </DVDS>
</LIBRARY>

Main.java class

package xpathexample;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

/**
*
* @author tunatore
*/
public class Main {

public static void main(String[] args) throws Exception {

//loading the XML document from a file
DocumentBuilderFactory builderfactory = DocumentBuilderFactory.newInstance();
builderfactory.setNamespaceAware(true);

DocumentBuilder builder = builderfactory.newDocumentBuilder();
Document xmlDocument = builder.parse(
new File(Main.class.getResource("books.xml").getFile().replace("%20", " ")));

XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
XPath xPath = factory.newXPath();

//getting the name of the book having an isbn number == ABCD7327923
String isbnNum = "ABCD7327923";
XPathExpression xPathExpression = xPath.compile("//LIBRARY//BOOKS//BOOK[@isbn='" +isbnNum + "']//NAME");
String nameOfTheBook = xPathExpression.evaluate(xmlDocument,XPathConstants.STRING).toString();

System.out.println("name of the Book ---> " + nameOfTheBook);

//getting all authors for the book having an isbn number == ABCD7327923
xPathExpression = xPath.compile("//LIBRARY//BOOKS//BOOK[@isbn='" +isbnNum + "']//AUTHORS//AUTHOR");

NodeList nodeListBook =  (NodeList) xPathExpression.evaluate(xmlDocument,XPathConstants.NODESET);
System.out.println("Total number of authors for the book " + nameOfTheBook + " is --> " + nodeListBook.getLength());

for (int index = 0; index < nodeListBook.getLength(); index++) {
String author = nodeListBook.item(index).getTextContent();
System.out.println("Author " + (index + 1) + " ---> " + author);
}

//getting all tags for the dvd having id == 3213324
String dvdID = "3213324";
xPathExpression = xPath.compile("//LIBRARY//DVDS//DVD[@id='" +dvdID + "']");
NodeList nodeDVD =  (NodeList) xPathExpression.evaluate(xmlDocument,XPathConstants.NODESET);
NodeList nodeListForDVD = nodeDVD.item(0).getChildNodes();

for (int index = 0; index < nodeDVD.item(0).getChildNodes().getLength(); index++) {
String tagName = nodeListForDVD.item(index).getNodeName();
String tagValue = nodeListForDVD.item(index).getTextContent();
if(!tagName.equals("#text")) //handling empty comment (#text)
System.out.println(tagName + " value ---> " + tagValue);
}
}
}

The output of the program;

run:
name of the Book —> Java Programing
Total number of authors for the book Java Programing is –> 3
Author 1 —> Tuna TORE
Author 2 —> Linus Torvalds
Author 3 —> James Gosling
NAME value —> Natural Science
CONTENT value —> Science
AUTHORS value —>
John Green
Bill Gates

BUILD SUCCESSFUL (total time: 2 seconds)

Download Source