BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 03-03-2009, 08:45 PM   #1
plevintampabay
Thumbs Must Hurt
 
Join Date: Jun 2008
Model: 8820
PIN: N/A
Carrier: AT&T
Posts: 72
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, 08:50 PM   #2
zechariahs
Thumbs Must Hurt
 
Join Date: Nov 2008
Location: Sioux Falls, SD
Model: 9530
PIN: N/A
Carrier: Verizon Wireless
Posts: 65
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, 09:04 PM   #3
plevintampabay
Thumbs Must Hurt
 
Join Date: Jun 2008
Model: 8820
PIN: N/A
Carrier: AT&T
Posts: 72
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

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

Forum Jump


Genuine OEM Dell 2375 B2375dnf B2375dfw 110V Fuser fixing N41P2 sku 724-BBCI picture

Genuine OEM Dell 2375 B2375dnf B2375dfw 110V Fuser fixing N41P2 sku 724-BBCI

$94.99



Genuine Dell OEM G Series G7 7790 CPU and Graphics Heatsink Assembly XRF05 picture

Genuine Dell OEM G Series G7 7790 CPU and Graphics Heatsink Assembly XRF05

$20.30



OEM Dell latitude E7250 CPU Cooling Fan with Heatsink DP/N 04T1K3 0J3M4Y picture

OEM Dell latitude E7250 CPU Cooling Fan with Heatsink DP/N 04T1K3 0J3M4Y

$13.90



1PCS NEW FIT FOR OEM Dell AC Adapter Dell Alienware DA330PM190 LA330PM190 330W picture

1PCS NEW FIT FOR OEM Dell AC Adapter Dell Alienware DA330PM190 LA330PM190 330W

$117.97



OEM Dell XPS 8910 8920 8930 Alienware Aurora R5 R6 R7 Front Cooling Fan 7M0F5 picture

OEM Dell XPS 8910 8920 8930 Alienware Aurora R5 R6 R7 Front Cooling Fan 7M0F5

$12.11



DELL 330-6581 3306581 725-10229 OEM LAMP FOR 1510X 1610HD 1610X  - Made By DELL picture

DELL 330-6581 3306581 725-10229 OEM LAMP FOR 1510X 1610HD 1610X - Made By DELL

$39.98







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