09-02-2010 03:54 AM
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
< TagOne />
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
09-02-2010 06:29 AM
You are missing a text node
Element TagOne = myDocument.createElement("TagOne");
Text TagOneText = myDocument.createTextNode("1");
then add the tag one element to the root.
09-03-2010 03:02 PM
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).
12-14-2011 09:00 AM
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.