• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Element Value and its respective values

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic