BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 03-03-2009, 07:45 PM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: Jun 2008
Model: 8820
PIN: N/A
Carrier: AT&T
Posts: 72
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default how do I write an XML DOM to a String or File?

Please Login to Remove!

I need to write an org.w3c.dom.Document to a String. I have a feeling I'll be using net.rim.device.api.xml.jaxp.XMLWriter, but the small example given in the API page on how to use that class, uses a SAXParser and an inputStream - which has nothing to do with a DOM.
Offline  
Old 03-05-2009, 07:50 PM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Nov 2008
Location: Sioux Falls, SD
Model: 9530
PIN: N/A
Carrier: Verizon Wireless
Posts: 65
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've been using the SaxParser.

SaxHandler sample:

Code:
package com.schwenkconsulting.blackping.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ResultHandler extends DefaultHandler
{
	private boolean m_bInMessage	= false;
	private boolean m_bInKey		= false;
	
	private String m_sStatus;
	private String m_sMessage;
	private String m_sKey;
	
	public void startDocument() throws SAXException
	{
	}

	public void endDocument() throws SAXException 
	{
	     // Nothing to do
	}

/** Gets be called on opening tags like:
 * <tag>
 * Can provide attribute(s), when xml was like:
 * <tag attribute="attributeValue">*/

	public void startElement(String namespaceURI, String localName,
	          String qName, Attributes atts) throws SAXException 
	{
		if(localName.equalsIgnoreCase("rsp"))
		{
			String sStatus = atts.getValue("status");
			setStatus(sStatus);
	    }
		else if(localName.equalsIgnoreCase("message"))
		{
			m_bInMessage = true; 
		}
		else if(localName.equalsIgnoreCase("key"))
		{
			m_bInKey = true;
		}
	}
	
	public void characters(char ch[], int start, int length) 
	{
        if(this.m_bInMessage)
        {
        	setMessage(new String(ch, start, length));
        }
        else if(m_bInKey)
        {
        	setKey(new String(ch, start, length));
        }
	}

/** Gets be called on closing tags like:
 * </tag> */

	public void endElement(String namespaceURI, String localName, String qName) throws SAXException 
	{
		if(localName.equalsIgnoreCase("message"))
		{
			m_bInMessage = false; 
		}
		else if(localName.equalsIgnoreCase("key"))
		{
			m_bInKey = false; 
		}
	}

	public String getStatus()
	{
		return m_sStatus;
	}
	
	public void setStatus(String status)
	{
		m_sStatus = status;
	}

	public String getMessage()
	{
		return m_sMessage;
	}

	public void setMessage(String message)
	{
		m_sMessage = message;
	}

	public String getKey()
	{
		return m_sKey;
	}

	public void setKey(String key)
	{
		m_sKey = key;
	}
}
Reading XML From an Input Stream

Code:
public static void parseXML(HttpConnection a_oCon, DefaultHandler a_oSAXHandler) throws IOException, ParserConfigurationException, SAXException
	{
		InputStream s = null;
		StringBuffer sbXMLResult = new StringBuffer();
		try 
		{
			s = a_oCon.openInputStream();
			int ch;
			while ((ch = s.read()) != -1) 
			{
				char cChar = (char) ch;
				sbXMLResult.append(cChar);
			}
		} 
		finally 
		{
			if (s != null)
				s.close();
		}
		
		ByteArrayInputStream bs = new ByteArrayInputStream(sbXMLResult.toString().getBytes());
		SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
		saxParser.parse(bs, a_oSAXHandler);
		
	}
__________________
Handspring >> Palm >> BB Pearl >> BB Storm

Tumblog: http://www.geekkink.com
Blog: http://www.zechariahs.org/blog
Offline  
Old 03-05-2009, 08:04 PM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: Jun 2008
Model: 8820
PIN: N/A
Carrier: AT&T
Posts: 72
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for the reply zechariahs.
Your's is an excellent reply.
To be completely accurate to what I wanted, one would have to implement a TreeWalker - something to traverse the nodes of the DOM, calling a handler, similiar to the one you wrote.

It so happens that since my development is targeting 4.2 devices (older devices like 8820), some of the XML packages in 4.7 aren't available to me.
As a result, I just finished implementing my own DOM, TreeWalker, Renderer and several other XML related components. Everything finally builds. Tomorrow I'll start testing and debugging.
Offline  
Closed Thread


Thread Tools

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.