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
mbasheerk
Posts: 326
Registered: ‎02-02-2009
My Device: Not Specified

create an XML using RIM API with a given encoding

I need to create an XML using RIM API; the following are the samples code I used for it.

 

DocumentBuilderFactory dBFactory = DocumentBuilderFactory

.newInstance();

DocumentBuilder dBuilder = dBFactory.newDocumentBuilder();

Document myDocument = dBuilder.newDocument();

 

Element Root = myDocument.createElement("Root");

Element TagOne = myDocument.createElement("TagOne");

TagOne.setNodeValue("1");

 

Root.appendChild(TagOne);

myDocument.appendChild(Root);

 

ByteArrayOutputStream  os = new ByteArrayOutputStream();

XMLWriter writer = new XMLWriter(os);

writer.setPrettyPrint();

DOMInternalRepresentation.parse(myDocument, writer);

String myXML = new String(os.toByteArray());

 

 

 

And I got the result XML like

 

<?xml version="1.0"?>

<Root>

< TagOne />

</Root>

 

 

I have two doubts regarding this..

 

Why the value not set for “TagOne” like <TagOne>1</TagOne>

How we can set an encoding to this XML like  <?xml version="1.0" encoding="UTF-8"?>

 

I am using JDE 4.2.1

Is there any easy way for create XML with a given encoding, can any one provide me a sample code will deeply appreciated

Developer
BB-Dude
Posts: 516
Registered: ‎07-23-2010
My Device: 9900

Re: create an XML using RIM API with a given encoding

You are missing a text node

 

Element TagOne = myDocument.createElement("TagOne");

Text TagOneText = myDocument.createTextNode("1");

TagOne.appendChild(TagOneText);

 

then add the tag one element to the root.

Developer
mbasheerk
Posts: 326
Registered: ‎02-02-2009
My Device: Not Specified

Re: create an XML using RIM API with a given encoding

thanks for your kind help

how i can give encoding like "utf-8" for this xml....

Developer
BB-Dude
Posts: 516
Registered: ‎07-23-2010
My Device: 9900

Re: create an XML using RIM API with a given encoding

yourstring.getBytes("UTF-8");

String string = new String(byte[], "UTF-8");

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: create an XML using RIM API with a given encoding

I think you cannot tell XMLWriter what encoding to use. Theoretically, if there is no other specification for the encoding, XML documents are interpreted as UTF-8 or UTF-16 (a byte order mark at the start, or the absence of one, determines which). If you want something else, I think you're out of luck if you want to stick with XMLWriter.

 

I have no idea if XMLWriter actually uses UTF-8. I've never experimented with it. I wouldn't be surprised if it uses ISO-8859-1 (which would be rather unfortunate).




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
New Contributor
Iulia_STANA
Posts: 2
Registered: ‎12-14-2011
My Device: Developer

Re: create an XML using RIM API with a given encoding

I've been playing around with this for a bit now. I have created a Document with the DocumentBuilder, added elements in it, and when I ask getXMLEncoding() I get null.

 

/Iulia