LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-07-2011, 05:37 AM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
Parsing XML using Sax Parser in Java


I'll show the code first:

Code:
class MyHandler extends DefaultHandler {
	
	public void startElement(String namespaceURI, String sName, String qName, Attributes atts) throws SAXException
	{
		if(qName.equals("command"))
		{
			System.out.println("wohoo");
		}
	}
	
	public void endElement(String uri, String localName, String qName) throws SAXException
	{
		
	}
	
	public void characters(char[] ch, int start, int length) throws SAXException 
	{
		
	}
}


public class XMLParser {

	String xmlfile = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<root>" + 
   "<command action=\"send\" id=\"1234\" [reconnect_after = 0] >" +
       "<clip id=\"1\">PD94bWwgdmVyc2lvbj0iMS4wIiBlb</clip>" + 
       "<clip id=\"2\">kjshdf23987iewjfnb892q34qdhadhax</clip>" +
   "</command>" + 
"</root>";
	
	public static void main(String[] args) 
	{
		MyHandler handler = new MyHandler();
		
		SAXParserFactory saxFactory = SAXParserFactory.newInstance();
		
		try {
			SAXParser saxParser = saxFactory.newSAXParser();
			XMLParser x = new XMLParser();
			saxParser.parse(new InputSource(new StringReader(x.xmlfile)), handler);
		}
		catch(SAXException e) {}
		catch(ParserConfigurationException e){}
		catch(IOException e){}

	}

}
What I am after is to get the string text from the clip tags. But for now I just tested to see if it can finds the command tags and print something if it does. But it doesn't find it. Anyone knows why ?

edit: looks like the xml is not good, i test it with a xml validator:

Code:
Element type "command" must be followed by attribute specifications: ">" or "/>".

Last edited by vbx_wx; 07-07-2011 at 06:17 AM.
 
Old 07-07-2011, 12:17 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Quote:
Originally Posted by vbx_wx View Post
I'll show the code first:

Code:
...
	String xmlfile = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<root>" + 
   "<command action=\"send\" id=\"1234\" [reconnect_after = 0] >" +
       "<clip id=\"1\">PD94bWwgdmVyc2lvbj0iMS4wIiBlb</clip>" + 
       "<clip id=\"2\">kjshdf23987iewjfnb892q34qdhadhax</clip>" +
   "</command>" + 
"</root>";
...
		catch(SAXException e) {}
		catch(ParserConfigurationException e){}
		catch(IOException e){}
edit: looks like the xml is not good, i test it with a xml validator:
Yeah, what is that reconnect_after thing? It would probably be a good idea to put something in your exception handlers so that errors are not silently ignored.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java xml parser keeps file locked after SAXException Wim Sturkenboom Programming 3 02-24-2011 10:32 AM
[SOLVED] Is *libqt4-xml* equivalent of *libxml2* with respect to *XML* parsing ? TheIndependentAquarius Linux - Software 1 11-25-2010 08:34 PM
[SOLVED] java xml parsing Wim Sturkenboom Programming 8 10-02-2009 04:35 AM
checking for XML::Parser... configure: error: XML::Parser perl module is required for kornerr Linux - General 11 11-16-2008 08:24 AM
XML Parser bulkman Linux - Software 6 04-29-2005 08:01 AM


All times are GMT -5. The time now is 04:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration