Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

saxParser

 
Siddhesh Khedekar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siddhesh Khedekar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mazer Lao Tzu : thanks your solution has worked
 
Siddhesh Khedekar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic