Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within XML
Search Coderanch
Advance search
Google search
Register / Login
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
Forum:
XML and Related Technologies
Help needed in debugging the XMLParser Application using SAX
Sakthi Ramasamy
Greenhorn
Posts: 17
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
My Intended output is
Sample Output:
Centrifugal | Booster Pumps and Systems | 1PGG7 | 10149_BRAND | 2PC24_AS01 | 0123
XML :
<?xml version="1.0" encoding="UTF-8"?> <Products> <Product ID="1PGG7" UserTypeID="Stock Number"> <Name>Centrifugal</Name> <Product ID="15951_DIV1" UserTypeID="Level_3"> <Name>Booster Pumps and Systems</Name> <Product ID="1PGG7" UserTypeID="Stock Number"> <Name>1PGG7</Name> <ProductCrossReference ProductID="10149_BRAND" Type="Brand"></ProductCrossReference> <AssetCrossReference AssetID="2PC24_AS01" Type="Primary Image"></AssetCrossReference> <Values> <Value AttributeID="CCPN">0123</Value> </Values> </Product> </Product> </Product> </Products>
My
Java
Code is:
GSAXParser.java import java.io.*; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.xml.parsers.ParserConfigurationException; 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 GSAXParser extends DefaultHandler{ List productList; String name,productID,assetID,type,ccpn; private String tempVar; private Data tempData; public GSAXParser() { System.out.println("Inside Constructor"); productList=new ArrayList(); } public void startElement(String URL, String lName,String qName, Attributes attr) throws SAXException{ tempVar=""; System.out.println("Inside Start element"); if(qName.equalsIgnoreCase("Product")){ tempData=new Data(); tempData.setID(attr.getValue("ID")); } else if(qName.equalsIgnoreCase("ProductCrossReference")){ tempData=new Data(); tempData.setProductID(attr.getValue("ProductID")); }else if(qName.equalsIgnoreCase("AssetCrossReference")){ tempData=new Data(); tempData.setAssetID(attr.getValue("AssetID")); }else if(qName.equalsIgnoreCase("Value")){ tempData=new Data(); tempData.setCcpn(attr.getValue("AttributeID")); } } public void characters(char[] ch, int start, int length) throws SAXException { System.out.println("Inside Characters"); tempVar = new String(ch,start,length); } public void endElement(String URL, String lName,String qName) throws SAXException{ System.out.println("Inside EndElement"); if(qName.equalsIgnoreCase("Value")){ productList.add(tempData); }else if (qName.equalsIgnoreCase("AttributeID")) { tempData.setCcpn(tempVar); } } public static void main(String argv[]){ System.out.println("Inside mainmethod"); GSAXParser gsp = new GSAXParser(); // Use the default (non-validating) parser gsp.readInput(); gsp.printOutput(); } private void printOutput() { // TODO Auto-generated method stub Iterator it=productList.iterator(); while(it.hasNext()){ System.out.println(it.next().toString()); } } private void readInput() { // TODO Auto-generated method stub SAXParserFactory factory = SAXParserFactory.newInstance(); try { //get a new instance of parser SAXParser sp = factory.newSAXParser(); //parse the file and also register this class for call backs sp.parse("Products.xml",this); //sp.parse("Electrical-PlugsAndReceptacals-StraightBladeDevices-20100504-1.xml", this); }catch(SAXException se) { se.printStackTrace(); }catch(ParserConfigurationException pce) { pce.printStackTrace(); }catch (IOException ie) { ie.printStackTrace(); } } } Data.java public class Data { private String ID; private String name; private String productID; private String assetID; private String supportingAssetID; private String ccpn; public Data() { super(); } public Data(String ID, String name, String productID, String assetID, String supportingAssetID, String ccpn) { super(); this.ID = ID; this.name = name; this.productID = productID; this.assetID = assetID; this.supportingAssetID = supportingAssetID; this.ccpn = ccpn; } public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getProductID() { return productID; } public void setProductID(String productID) { this.productID = productID; } public String getAssetID() { return assetID; } public void setAssetID(String assetID) { this.assetID = assetID; } public String getSupportingAssetID() { return supportingAssetID; } public void setSupportingAssetID(String supportingAssetID) { this.supportingAssetID = supportingAssetID; } public String getCcpn() { return ccpn; } public void setCcpn(String ccpn) { this.ccpn = ccpn; } public String toString(){ StringBuffer sb= new StringBuffer(); sb.append("ID :"+getID()+" | "); sb.append("Name :"+getName()+" | "); sb.append("productID :"+getProductID()+" | "); sb.append("assetID :"+getAssetID()+" | "); sb.append("supportingAssetID :"+getSupportingAssetID()+" | "); sb.append("ccpn :"+getCcpn()+" | "); return sb.toString(); } }
Could anybody please debug it for me...I am getting null for everything...
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Problem in parsing an XML tag.
Search and Replace tag (Not its Value) in an XML file.
I need to set <s:property> value in the action class
need to transfer productID from url to action class in struts 2
Missing characters with SAX Parser
More...