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
Developer
tejaswiyvs
Posts: 55
Registered: ‎02-08-2010
My Device: Blackberry Bold
Accepted Solution

Parsing XML in a string with kXML

Hi,

 

I'm making a web service call which returns XML in a string. I need to parse this using kXML. The setup is done, but I can't find any help regarding parsing the string.

 

Any help?

 

Thanks,

Teja.

Developer
tejaswiyvs
Posts: 55
Registered: ‎02-08-2010
My Device: Blackberry Bold

Re: Parsing XML in a string with kXML

[ Edited ]

Okay I've come to this point.


ASP.NET Web service returns an XML string (Contains \n and \r characters, need to clean it up).

 

And here's the sample code that I have so far. Seems to work, so posting it for reference -

 

// Say str = return from the Webservice. Using Plain HTTP POST for the response.

 

xmlByteArray = str.getBytes();
ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlByteArray);
InputStreamReader xmlReader = new InputStreamReader(xmlStream);
KXmlParser parser = new KXmlParser();
parser.setInput(xmlReader);
parser.nextTag();
//parser.require(XmlPullParser.START_TAG, null, "facility");
String[] xmlNodes = new String[10];
int i=0;
String str1;
while(parser.next() != XmlPullParser.END_DOCUMENT) {
if((str1 = parser.getName())!=null) {
xmlNodes[i] = str1;
}
i++;
}

 

xmlByteArray = str.getBytes();

ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlByteArray);

InputStreamReader xmlReader = new InputStreamReader(xmlStream);

KXmlParser parser = new KXmlParser(); parser.setInput(xmlReader);

 parser.nextTag();

 //parser.require(XmlPullParser.START_TAG, null, "facility");

 

String[] xmlNodes = new String[10];

 int i=0;

 String str1;

 

while(parser.next() != XmlPullParser.END_DOCUMENT) {

  if((str1 = parser.getName())!=null) {

  xmlNodes[i] = str1;

i++;

  }

}

Developer
hades_6
Posts: 178
Registered: ‎08-26-2009
My Device: Not Specified

Re: Parsing XML in a string with kXML

Did you try this?

 

 

	try{
			HttpConnection connection = null;
			InputStream inputStream = null;
			
			connection = (HttpConnection) Connector.open(URL + MainUI.connectionParameters,Connector.READ_WRITE, true);
			inputStream = connection.openInputStream();
			

			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse( inputStream );
	
			Element rootElement = document.getDocumentElement();
			rootElement.normalize();
			displayNode( rootElement, 0 );
		} 
		catch ( Exception e ) 
		{
			System.out.println( e.toString() );
		}

 

private void displayNode( Node node, int depth ) 
	{        
		
		
		
		if ( node.getNodeType() == Node.ELEMENT_NODE ) 
		{
			StringBuffer buffer = new StringBuffer();
			//indentStringBuffer( buffer, depth );
			NodeList childNodes = node.getChildNodes();
			int numChildren = childNodes.getLength();
			Node firstChild = childNodes.item( 0 );
	
			// If the node has only one child and that child is a Text node, then it's of 
			// the form  <Element>Text</Element>, so print 'Element = "Text"'.
			
if ( numChildren == 1 && firstChild.getNodeType() == Node.TEXT_NODE ) 
			{
					
			
						buffer.append( firstChild.getNodeValue() );
						buffer.append( "\n" );
						add( new RichTextField( buffer.toString() ));
						add( new SeparatorField());
			
			} 
			else 
			{
		
				parentName = node.getNodeName();
	
				for ( int i = 0; i < numChildren; ++i ) 
				{
					displayNode( childNodes.item( i ), depth + 1 );
				}
			}
		} 
		else 
		{
	
			String nodeValue = node.getNodeValue();
			if ( nodeValue.trim().length() != 0 ) 
			{
				StringBuffer buffer = new StringBuffer();
				indentStringBuffer( buffer, depth );
				buffer.append( '"' ).append( nodeValue ).append( '"' );
				add( new RichTextField( buffer.toString() ));
			}
		}
	}

 

 

Developer
tejaswiyvs
Posts: 55
Registered: ‎02-08-2010
My Device: Blackberry Bold

Re: Parsing XML in a string with kXML

Thanks, this looks great, but I've got the kXML working with some really bad coding. I'll probably come back to use this after I make the deadline.

New Contributor
eesbee
Posts: 9
Registered: ‎05-11-2010
My Device: 8800

Re: Parsing XML in a string with kXML

I'd like to know the library you are using... I have problem with the NodeList API.

is that a ksoap2?.. and its package name and classes.

 

THanks and Regards!