Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa XML Problem facing in SAX Parsing
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


sunil
Problem facing in SAX Parsing
1 Answer(s)      4 years and 10 months ago
Posted in : XML


I have facing the issue in SAX Parsing like i have got the xml and the xml structure is like below.

<?xml version="1.0" encoding="UTF-8" ?>
- <Customers>
- <Customer>
  <CustName>M.T.C ALUPACK(MOHAMED ALTURK & CO)</CustName>
  <CustId>06684209932</CustId>
  <CountryCode>BH</CountryCode>
  <BranchCode>06684</BranchCode>
  <AccountNbrs />
  </Customer>
- <Customer>
  <CustName>AL HASSANAIN CO.WLL</CustName>
  <CustId>06684210088</CustId>
  <BranchCode>06684</BranchCode>
- <AccountNbrs>
  <AccountNbr>0668421008800119BHD</AccountNbr>
  <AccountNbr>0668421008800119USD</AccountNbr>
  <AccountNbr>0668421008800216BHD</AccountNbr>
  <AccountNbr>0668421008800119QAR</AccountNbr>
  </AccountNbrs>
  </Customer>
<Customer>
  <CustName>M.T.C. ALUPACK (MOHAMED AL TURK &</CustName>
  <CustId>06684209916</CustId>
  <CountryCode>BH</CountryCode>
  <BranchCode>06684</BranchCode>
- <AccountNbrs>
  <AccountNbr>0668420991600115BHD</AccountNbr>
  <AccountNbr>0668420991600115EUR</AccountNbr>
  <AccountNbr>0668420991600115USD</AccountNbr>
  </AccountNbrs>
  </Customer>
- <Customer>
  <CustName>AL ZAIN AUTO SUPPLIES ESTABLISHMENT</CustName>
  <CustId>06684209577</CustId>
  <BranchCode>06684</BranchCode>
- <AccountNbrs>
  <AccountNbr>0668420957700159BHD</AccountNbr>
  <AccountNbr>0668420957700159EUR</AccountNbr>
  <AccountNbr>0668420957700159USD</AccountNbr>
  </AccountNbrs>
  </Customer>
<Customers>

you can find the above xml there is no CountryCode in the second record and 4th records.
if that CountryCode is not present then i need to skip the total Customer tag.
so we were using the sax parcing in our application. can you please let me know... how to handle this code in SAX Parcing.

Advertisement
View Answers

March 4, 2010 at 4:32 PM


Hi Friend,

First of all remove & sign from your xml file,it contradicts.And then try the following code:

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
import java.util.*;

public class XMLParser{
Date date=new Date();
public static void main(String[] args) throws IOException{
XMLParser detail = new XMLParser("C:/customers.xml");
}
public XMLParser(String str){
try{
File file = new File(str);
if (file.exists()){
SAXParserFactory parserFact = SAXParserFactory.newInstance();
SAXParser parser = parserFact.newSAXParser();
System.out.println("XML Data: ");
DefaultHandler dHandler = new DefaultHandler(){
boolean name;
boolean id;
boolean ccode;
boolean bcode;
boolean ano;

public void startElement(String uri, String localName,String element_name, Attributes attributes)throws SAXException{
if (element_name.equals("CustName")){
name = true;
}
if (element_name.equals("CustId")){
id = true;
}
if (element_name.equals("CountryCode")){
ccode = true;
}
if (element_name.equals("BranchCode")){
bcode = true;
}
if (element_name.equals("AccountNbr")){
ano = true;
}
}
public void characters(char[] ch, int start, int len) throws SAXException{
String str = new String (ch, start, len);

if (name){
System.out.println("Cust Name: "+str);
name = false;
}
if (id){
System.out.println("Cust ID: "+str);
id = false;
}
if (ccode){
System.out.println("Country Code: "+str);
ccode = false;
}
if (bcode){
System.out.println("Branch Code: "+str);
bcode = false;
}
if (ano){
System.out.println("AccountNbrs: "+str);
ano = false;
}

}
};
parser.parse(str, dHandler);
}
else{
System.out.println("File not found!");
}
}
catch (Exception e){

}
}
}

Thanks


Related Tutorials/Questions & Answers:
Problem facing in SAX Parsing - XML
Problem facing in SAX Parsing  I have facing the issue in SAX Parsing... tag. so we were using the sax parcing in our application. can you please let me know... how to handle this code in SAX Parcing.   Hi Friend, First
Problem facing in SAX Parsing - XML
Problem facing in SAX Parsing  I have facing the issue in SAX Parsing... then i need to skip the total Customer tag. so we were using the sax parcing in our application. can you please let me know... how to handle this code in SAX
Advertisements
parsing xml using sax
parsing xml using sax  how to get values if the root elements are same and their attributes are different
Truncating issue while parsing XML with SAX Parser.
Truncating issue while parsing XML with SAX Parser.  I am Parsing one xml file and after parsing I have to enter the data into the database using hibernate. The problem is while parsing some elements, its not getting the complete
XML Parsing Using Sax Parser in J2ME for serverside
XML Parsing Using Sax Parser in J2ME for serverside  Hai team, i have doubt in Parsing using sax parser in serverside plz help me for xml parsing in j2me using sax parser on server side thanks in advance... regards Selva
XML Parsing Using Sax Parser in J2ME
XML Parsing Using Sax Parser in J2ME  Hai team, I hope you, You should help me, I have trouble in Xml parsing... I have decoded value, what... to do xml parsing help me for that... Regards Alagu
parsing word xml file using SAX parser - XML
parsing word xml file using SAX parser  i am parsing word 2003's XML file using SAX.here my question is,i want to write some tag elements which are between in other tag to a file. For ex
facing problem plz help me out - Framework
Facing problem plz help me out  hi i am new to servlet i deployed... the web.xml file too parallel to the classes folder now i am facing this problem.plz tell me what to do... error:The requested resource (Servlet servlet
Parsing repeatitive xml elements using SAX Parser - XML
Parsing repeatitive xml elements using SAX Parser  Hi, I'm using SAX Parser to read an XML file. My XML file contains repeatitive tags. I'm... SAX parser. Thanks. Thanks.  Hi Shailendra, Use the following
Facing Problem to insert Multiple Array values in database - JSP-Servlet
Facing Problem to insert Multiple Array values in database  Hai... facing the problem while inserting the data in database. iam using the MsAccess... Manoj   Hi friend, You Have Problem in Data Table and check
Facing Problem with submit and cancel button in same page - Struts
Facing Problem with submit and cancel button in same page  Hi, can u please help me out.I have placed submit and cancel button in the jsp page but i am unable to know how to write the form and action classes   Hi
Java XML Parsing Using SAX
Java XML Parsing Using SAX To Parse XML document using SAX parser method you.... At first Create the SAX Parser as // getting SAXParserFactory instance... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse method
facing problem while retrive value from Post textarea
facing problem while retrive value from Post textarea  Hi this is subha. I face a small problem while retriving value of the textbox in the java script.I use struts framework and this is my code for post textarea. <tr>
Facing - Ajax
Facing   Hello All, i m using ajax in my application i m fetching data from db using ajax method and returning response in xml format. But when... the problem. once check u r xml response format. xml response should be like
DOM to SAX and SAX to DOM - XML
Java DOM to SAX and SAX to DOM  Simple API for XML and Document Object Model
DOM to SAX and SAX to DOM - XML
DOM SAX Parser Java  What is the Difference between SAX and DOM parsers
XML parsing to Mysql database
XML parsing to Mysql database  Can someone please post the code for parsing an XML file into Mysql database using SAX
sax parser for xml
sax parser for xml  sax parser code that works with any XML i.e independent of any XML to extract data from XML
parsing XML file to get java object - XML
parsing XML file to get java object  Hello, I'm facing a problem in parsing XML file to get the java object. I've tried to retrieve data from XML file using SAX parser. my XML file structure is the following
The Simple API for XML (SAX) APIs
The Simple API for XML (SAX) APIs       The SAX Packages: The SAX parser is defined in the following... Defines the SAX interfaces. The name "org.xml" is the package
SAX Parser
SAX Parser       The Simple API for XML (SAX) is a serial access parser API for XML. It is used... executes, the SAX parser recognizes and responds to each XML structure taking
SAX Parser for huge XML file
SAX Parser for huge XML file  Hi.... if the XML file is small and repetitive then this will have meaning.. import javax.xml.parsers.... using sax parser... i wanna use direct file path... not by defining each and every
XML parsing using Java - XML
" RenewalRate1="0.03100". I'm facing problem switching between tables. Like...XML parsing using Java  I'm trying to parse a big XML file in JAVA. The goal is like i will take console input from user until "Coverage
parsing xml file in jsp
parsing xml file in jsp  example that pars XML file in JSP
parsing xml with jquery
parsing xml with jquery  Please explain how to parse a xml file into jquery. Thanks
Parsing string in objective c
Parsing string in objective c  Hi, How can i parse a string in Objective c?? Thanks.   Parsing string in objective c This example will also help you to separate the strings separated by one component
Parsing date in Java
Parsing date in Java  How can i parse the date in Java in a simple format..?   SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy
Parsing into date object
Parsing into date object  Here is my code: String de = (String) session.getAttribute("licvalid"); DateFormat df = new SimpleDateFormat("yyyy/MM/dd.... But, it is showing error at the line where i performed parsing operation. Please help me
Parsing into date object
Parsing into date object  Here is my code: String de = (String) session.getAttribute("licvalid"); DateFormat df = new SimpleDateFormat("yyyy/MM/dd.... But, it is showing error at the line where i performed parsing operation. Please help me
Html Parsing Extracting Content
Html Parsing Extracting Content  Hello i need to parse html and search for a string and get its attributes.For example 'xxx' is my search string. i search html page and find xxx css attributes.Java,php,javascript is okey thank
Advertisements
 

 

 

DMCA.com