File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
*
The moose likes XML and Related Technologies and the fly likes Element Value and its respective values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Element Value and its respective values" Watch "Element Value and its respective values" New topic
Author

Element Value and its respective values

karthik swamy
Ranch Hand

Joined: Mar 14, 2011
Posts: 42
Hi All,
Following is my xml read and display code:

package com.mycompany.xmlread;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class App
{
public static void main( String[] args )
{
try {

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();

DefaultHandler handler = new DefaultHandler() {

boolean jname = false;
boolean con = false;
boolean driverName = false;
boolean username = false;
boolean password = false;

@Override
public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {

//System.out.println("Start Element :" + qName);

if (qName.equalsIgnoreCase("jndi-name")) {
jname = true;
}

if (qName.equalsIgnoreCase("connection-url")) {
con = true;
}

if (qName.equalsIgnoreCase("driver-class")) {
driverName = true;
}

if (qName.equalsIgnoreCase("user-name")) {
username = true;
}
if (qName.equalsIgnoreCase("password")) {
password = true;
}
}

@Override
public void endElement(String uri, String localName,
String qName) throws SAXException {

//System.out.println("End Element :" + qName);

}

@Override
public void characters(char ch[], int start, int length) throws SAXException {
String str=new String(ch, start, length);
if(str.equals("abc")){
/*
Made all boolean values false;

*/

}
if (jname) {
System.out.println("JNDI Name : " +str );
jname = false;
}

if (con) {
System.out.println("Connection URL : " +str);
con = false;
}

if (driverName) {
System.out.println("Driver Name : " +str);
driverName = false;
}

if (username) {
System.out.println("Username : " + str);
username = false;
}
if (password) {
System.out.println("Password : " +str);
password = false;
}
}

};

saxParser.parse("D:\\test.xml", handler);

} catch (Exception e) {
e.printStackTrace();
}
}

}

XML file:

<?xml version='1.0' encoding='us-ascii'?>

<datasources>
<local-tx-datasource>
<jndi-name>abc</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/jboss</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>abc1</user-name>
<password>1234</password>
</local-tx-datasource>

<local-tx-datasource>
<jndi-name>xyz</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/jboss</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>xyz1</user-name>
<password>jan2010</password>
</local-tx-datasource>
</datasources>

I need to display only those values which comes under tag name : <jndi-name>xyz<jndi-name>
But still it prints all the values of xml file.So please help me out how can i put the check.


Thank In Advance,
Karthik G.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 547
    
    3
I need to display only those values which comes under tag name : <jndi-name>xyz<jndi-name>
But still it prints all the values of xml file.

If the way of conducting the logical reasoning for sax parsing's handler remains at the known-to-be-able level, it can be quite challenging when you actually need to do it without all reasoning seemed frozen up...

This is how you can consider: focusing your attention also on the wrapper element of those data you want to extract, that is local-tx-datasource. Make up a flag to decide whether to collect the data or not. Partial data are then cached or stored as the parsing getting its way linearly. The confirmation of data collection comes at the time the parser meets up jndi-name and gets its text being xyz (supposing though jndi-name tag is unique within the wrapper element - other you have to do more work.) Once it is confirmed, the data is poured out when the parser comes across the closing tag of the wrapper element. If the confirmation is negative, the data collection process is abandoned to safe up useless effort.

This is one way to implement it and how the handler would look like, based generally on you have at this stage.

That's the idea.
 
 
subject: Element Value and its respective values