aspose file tools*
The moose likes XML and Related Technologies and the fly likes saxParser 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 "saxParser" Watch "saxParser" New topic
Author

saxParser

Siddhesh Khedekar
Ranch Hand

Joined: Nov 09, 2009
Posts: 41
I have following logic for parsing XML -

SAX Parser class


My SaxHandler class



My XML

<?xml version="1.0"?>
<table>
<customer>
<firstName>Fernando</firstName>
<lastName>Torres</lastName>
<ssn>23324</ssn>
</customer>
<customer>
<firstName>william</firstName>
<lastName>gallas</lastName>
<age>33</age>
</customer>

</table>



I am getting output -

Start of Document
{firstName=william, lastName=gallas, ssn=23324, age=33}
{firstName=william, lastName=gallas, ssn=23324, age=33}
No. of Entries = 2
End of Document


But I want following output-

Start of Document
{firstName=Fernando, lastName=Torres, ssn=23324}
{firstName=william, lastName=gallas,age=33}
No. of Entries = 2
End of Document


Suggest the changes I should make to my code.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
In the future, please UseCodeTags. I've added them for you here. Without them, it's unnecessarily hard to read the code, making it less likely that people will bother to do so.

qName != "table"

There may be other problems, but strings are not compared using the != operator, they're compared using the equals method (which you're actually using at a different spot).

Your implementation of the characters method is flawed (although that's not the cause of the problem at hand). See the first entry in the XmlFaq for why it's incorrect.

Also, you should use the @Override annotation for ALL overridden methods.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

Your strategy should be: at the start of a Customer element, clear the data structure which will store your Customer data. (Or create a new one.)
Mazer Lao Tzu
Ranch Hand

Joined: Jan 20, 2010
Posts: 35
The immediate problem that you are having is that you keep adding the same object to the list. Here is a boiled down version of what your code does in this case:


In this example, as in your code, the object that is added to the list first is the exact same object that gets added to it the second time. After adding it to the list, the object is modified. Here is a solution to the example:


There are other faults present in the code you have shown (as other posters have pointed out), but this one is the cause of the bug you have noticed.



-- Mazer
Siddhesh Khedekar
Ranch Hand

Joined: Nov 09, 2009
Posts: 41
Mazer Lao Tzu : thanks your solution has worked
Siddhesh Khedekar
Ranch Hand

Joined: Nov 09, 2009
Posts: 41
My program is working fine, when I access from D:\...\... or O:\...\... or N:\...\...
Also I have the rights to access C: , but it gives me a following error when I access from C:\...\...

java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at ProcessServlets.fileparser(ProcessServlets.java:47)
at ProcessServlets.doGet(ProcessServlets.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Unknown Source)


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: saxParser