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



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


MOSFET - IRFZ44N 55V - Transistor  for Arduino Pi  TTL picture

MOSFET - IRFZ44N 55V - Transistor for Arduino Pi TTL

$54.79



MOSFET - IRF540N 100V 33A - Transistor for Arduino Pi TT picture

MOSFET - IRF540N 100V 33A - Transistor for Arduino Pi TT

$49.72



TO-92 Assortment NPN PNP DIY kit 15 value 600pcs Transistor  picture

TO-92 Assortment NPN PNP DIY kit 15 value 600pcs Transistor

$14.40



Portable 600pc 15Value NPN PNP Transistor TO-92 Assortment Kit Set /Box Hot picture

Portable 600pc 15Value NPN PNP Transistor TO-92 Assortment Kit Set /Box Hot

$12.66



US Stock 4pcs 2N3055 NPN AF Amp Audio Power Transistor 15A/60V picture

US Stock 4pcs 2N3055 NPN AF Amp Audio Power Transistor 15A/60V

$11.66



1 Pc. New 2SC2312 Transistor Silicon RF Power MITSUBISHI NPN 12V 17W 27MHz TO220 picture

1 Pc. New 2SC2312 Transistor Silicon RF Power MITSUBISHI NPN 12V 17W 27MHz TO220

$14.95







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