Can someone help me with this JAVA SAXParser?

I have been working for three hours and I cannot get this parser F ***** to work. Sorry for the curse. I donโ€™t understand why I canโ€™t find one worthy textbook that does exactly what I want.

I just want to send a String / XML function. Then analyze it. it is not that difficult. In python, I can do it with my eyes closed. Awful, free documentation right here: http://www.crummy.com/software/BeautifulSoup/documentation.html

import BeautifulSoup soup = BeautifulSoup(the_xml) persons_name = soup.findAll('first_name')[0].string 

Why can't I find good, simple documentation that teaches me to parse XML ????? This is my current code for JAVA SAX, and it does not work, and I donโ€™t even know why.

  public static void parseit(String thexml) { SAXParserFactory factory = SAXParserFactory.newInstance(); try { SAXParser saxParser = factory.newSAXParser(); saxParser.parse( thexml , new DefaultHandler() ); } catch (Throwable err) { err.printStackTrace (); } } 

Can someone just write me code for parsing XML using the SAX parser ... please ... It's just like 5 lines of code.

+2
source share
5 answers

So, you need to implement your own handler (instead of using the default). Therefore replace

 saxParser.parse( thexml , new DefaultHandler() ); 

from

  saxParser.parse( thexml , new MyFreakingHandler() ); 

where MyFreakingHandler implements the HandlerBase interface or can extend the DefaultHandler class. Then just do the implementation for methods like

 public void startDocument () throws SAXException public void endElement (String name) throws SAXException 

I do not know why you could not find any textbook on the Internet. I have not used SAXParser for at least 3 years, and in order to reply to your post, I just asked Google for help.

EDIT:

Alright, so clarify the situation. There used to be an official Java tutorial for SAX, which somehow I canโ€™t find on the Internet right now, but there are still a lot of decent informal tutorials that can be very useful. Try this, for example: http://www.java-samples.com/showtutorial.php?tutorialid=152

+3
source

You must extend your default handler DefaultHandler. For example, try the following:

  saxParser.parse( new InputSource(new StringReader(thexml)) , new DefaultHandler() { public void startElement(String uri, String localName, String qName, Attributes attributes) { System.out.println("Hello "+qName); } }); 
+3
source

You must extend DefaultHandler with your own implementation. Sax parser is good if you work with large documents. If not, you might be better off with another xml parser like dom4j.

Here is a simple saxophone

+2
source

I donโ€™t know if this is an option for you, but since Groovy and Java play well together, why not try one of Groovy for XML processing.

In particular, look at XML Slurper ( http://groovy.codehaus.org/Reading+XML+using+Groovy's+XmlSlurper )

 def records = new XmlSlurper().parseText(thexml) def persons_name = records.first_name[0] 

In my opinion, it is as close as you get to BeautifulSoup in a Java-compatible way.

0
source

Using the Java XPath API

 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xPathExpression = xPath.compile("//first_name"); NodeList nodes = (NodeList) xPathExpression.evaluate( new InputSource(new FileInputStream(the_xml)), XPathConstants.NODESET); 

Yes, this is overly detailed.

0
source

Source: https://habr.com/ru/post/1301091/


All Articles