Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
pgangal
Posts: 19
Registered: ‎11-25-2009
My Device: Not Specified

looking for method which converts a string into an xml doc

am using DOM parser ..it parses docs only as per my knowledge...pls correct me if am wrong..does antbody know how to convert a str into an xml doc...suggest if i shd use some other parser ..am writing code in java for blackberry

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: looking for method which converts a string into an xml doc

Don't know the DOM, I generally use the SAX parser, but for that to convert a String to input for the parser, I use code like:

 

new InputStreamReader( new ByteArrayInputStream(XMLMessage.getBytes()) )

Please use plain text.
New Developer
pgangal
Posts: 19
Registered: ‎11-25-2009
My Device: Not Specified

Re: looking for method which converts a string into an xml doc

Do you have any code/doc where I can read about SAX parser and its details or any sample code

Please use plain text.
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: looking for method which converts a string into an xml doc

class MySaxHandler extends DefaultHandler  
{
	 private Vector values = new Vector();
	 private boolean value = false;
	 String str;
	 public MySaxHandler(InputStream istream)  
	 {
	 	try{
//			 reportValues = new Vector();
			System.out.println("..............................Thread started..............................");
			XMLReader parser = XMLReaderFactory.createXMLReader();
			parser.setContentHandler(this);
		 	InputSource is = new InputSource(istream);
		 	parser.parse(is);
		 	System.out.println("Parsing over");
		 	istream.close();  
		 	System.out.println("stream closed");
		 	} catch (IOException e) {
				System.out.println(e.getMessage());
//				add(new LabelField(e.getMessage()));
			 
			} catch (SAXException e) {
				System.out.println(e.getMessage());
//				add(new LabelField(e.getMessage()));
			 
			} 
	 }
	public void startDocument() throws SAXException{ 
		// executes at the start of the document 
		System.out.println("Document started................");
	}
	public void endDocument() throws SAXException{ 
		// executes at the end of the document 
		System.out.println("Document ends................");
	}
	public void startElement(String nsURI, String strippedName,String tagName, Attributes attributes){
		// executes at the start of each tag 			
		 if(tagName.equalsIgnoreCase("Name")) value = true;	 
	}
	public void endElement(String namespaceURI, String strippedName, String tagName) throws SAXException{ 
		// executes at the end of each tag	     
		if(tagName.equalsIgnoreCase("Name"))value = false;
		}
	public void characters(char[] ch, int start, int length) {
		if(value){
			String str = new String(ch, start, length);
			System.out.println("Value is.........."+str);
			values.addElement(str);
		}
	}
}

 

this is the way i use SAX Parser....

 

 

Thanks and Regards,
PraveenGoparaju.
Please use plain text.