I guess you are trying to use a Transformer to convert a Document to
an xml String? Here's some more quick&dirty code for it:
public String getXml(Node node) {
StringBuilder buffer = new StringBuilder();
if (node == null) {
return "";
}
if (node instanceof Document) {
buffer.append(getXml(((CustomDocument)node).getFirstChild()));
} else if (node instanceof Element) {
Element element = (Element)node;
buffer.append("<");
buffer.append(element.getNodeName());
if (element.hasAttributes()) {
NamedNodeMap map = element.getAttributes();
for (int i = 0; i < map.getLength(); i++) {
Node attr = map.item(i);
buffer.append(attr.getNodeName());
buffer.append("=\"");
buffer.append(attr.getNodeValue());
buffer.append("\" ");
}
}
buffer.append(">");
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
buffer.append(getXml(children.item(i)));
}
buffer.append("</");
buffer.append(element.getNodeName());
buffer.append(">");
} else if (node != null && node.getNodeValue() != null) {
buffer.append(node.getNodeValue());
}
return buffer.toString();
}
On Mar 25, 8:23 am, "Vesin Duško" <[EMAIL PROTECTED]> wrote:
> Tnx Dan, i have another problem,
>
> Android does not have TransformerFactory class so how to get data from
> generated document?
>
> It would be nice if I can get at list some string that contains XML.
>
> On 24/03/2008, Dan U. <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > I didn't see a solution for the xml attribute namednodemap == null
> > issues, so I decided to go ahead and fix it with some custom classes.
> > I'm sure Google will get it fixed, but in case they don't in time for
> > the ADC submission, some people might want a solution now. It's real
> > quick&dirty, so if something doesn't work, that's why. Anyway, here
> > you go...
>
> > Two things to note:
>
> > 1. Keep the package names or it won't work.
> > 2. I'm no longer using a factory to build the documentbuilder. You can
> > probably change this, but it was simpler this way. Here's how you use
> > it:
> > DocumentBuilder builder = new CustomDocumentBuilder(); and then just
> > use it like a normal DocumentBuilder.
>
> > package android.xml.parsers;
> > import org.w3c.dom.Document;
> > import android.xml.dom.CustomDocument;
> > import android.xml.dom.DOMImplementationImpl;
>
> > public class CustomDocumentBuilder extends DocumentBuilderImpl {
>
> > public CustomDocumentBuilder() {
> > super();
> > }
>
> > @Override
> > public Document newDocument() {
> > return new
> > CustomDocument((DOMImplementationImpl)super.getDOMImplementation(),
> > null, null, null);
> > }
> > }
>
> > package android.xml.dom;
> > import org.w3c.dom.DOMException;
> > import org.w3c.dom.DocumentType;
> > import org.w3c.dom.Element;
>
> > public class CustomDocument extends DocumentImpl {
> > public CustomDocument(DOMImplementationImpl impl, String
> > namespaceURI,
> > String qualifiedName, DocumentType doctype) {
> > super(impl, namespaceURI, qualifiedName, doctype);
> > }
>
> > @Override
> > public Element createElement(String tagName) throws DOMException {
> > return new CustomElement(this, tagName);
> > }
>
> > @Override
> > public Element createElementNS(String namespaceURI, String
> > qualifiedName)
> > throws DOMException {
> > return new CustomElement(this, namespaceURI,
> > qualifiedName);
> > }
> > }
>
> > package android.xml.dom;
>
> > import org.w3c.dom.Attr;
> > import org.w3c.dom.DOMException;
> > import org.w3c.dom.Document;
> > import org.w3c.dom.NamedNodeMap;
>
> > public class CustomElement extends ElementImpl {
>
> > private NamedNodeMap mAttributes = new NamedNodeMapImpl();
>
> > public CustomElement(Document document, String namespaceURI,
> > String qualifiedName) {
> > super(document, namespaceURI, qualifiedName);
> > }
>
> > public CustomElement(Document document, String tagName) {
> > super(document, tagName);
> > }
>
> > @Override
> > public void setAttribute(String name, String value) throws
> > DOMException {
> > super.setAttribute(name, value);
> > mAttributes.setNamedItem(getAttributeNode(name));
> > }
>
> > @Override
> > public Attr setAttributeNode(Attr newAttr) throws DOMException {
> > mAttributes.setNamedItem(newAttr);
> > return super.setAttributeNode(newAttr);
> > }
>
> > @Override
> > public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
> > mAttributes.setNamedItemNS(newAttr);
> > return super.setAttributeNodeNS(newAttr);
> > }
>
> > @Override
> > public void setAttributeNS(String namespaceURI, String
> > qualifiedName,
> > String value) throws DOMException {
> > super.setAttributeNS(namespaceURI, qualifiedName, value);
> > mAttributes.setNamedItemNS
> > (getAttributeNodeNS(namespaceURI,
> > qualifiedName));
> > }
>
> > @Override
> > public NamedNodeMap getAttributes() {
> > return mAttributes;
> > }
>
> > @Override
> > public boolean hasAttributes() {
> > return mAttributes.getLength() > 0;
>
> > }
> > }
>
> > On Mar 24, 1:12 pm, "Dan U." <[EMAIL PROTECTED]> wrote:
> > > There definitely is an issue with attributes which won't let you
> > > access them unless you know their names beforehand. If that isn't a
> > > problem, then it's easy to write some code to give you back an Xml
> > > string from a Document.
>
> > > On Mar 24, 12:09 pm, "Dexter's Brain" <[EMAIL PROTECTED]> wrote:
>
> > > > Hey Vishal,
>
> > > > This code will work with JDK.....But, currently, there is some problem
> > > > with the android SDK.
>
> > > > The NamedNodeMap doesn't work....I dont know how did you test it on
> > > > android. There is also a bug reported for this issue with the SDK.. I
> > > > guess.....I tried using this, but it never worked for me....Lot of
> > > > other people are also having this problem......
>
> > > > Dexter.
>
> > > > vishal wrote:
> > > > > I think i have a code for this which is general with any DOM
> > document
> > > > > type...
>
> > > > > Here initially you have to pass root element. Plz tell me if im
> > > > > wrong.... and if errors in the code.. or any way to improve that
>
> > > > > private void addTagToTheFile(Element elem) throws IOException {
> > > > > NamedNodeMap elemAttributes = elem.getAttributes
> > ();
> > > > > StringBuffer elemTagTowrite = new StringBuffer();
> > > > > Node attribute ;
> > > > > elemTagTowrite.append("<"+elem.getTagName());
> > > > > if(elemAttributes!=null){
> > > > > Log.i(elem.getTagName
> > (),""+elemAttributes.getLength());
> > > > > int noOfAttributesInTag =
> > elemAttributes.getLength();
> > > > > for(int counter=0; counter <
> > noOfAttributesInTag; counter++){
> > > > > attribute = elemAttributes.item
> > (counter);
> > > > > elemTagTowrite.append(" " +
> > attribute.getNodeName() +
> > > > > " = \"" +
> > attribute.getNodeValue()+"\"");
> > > > > }
> > > > > }
> > > > > elemTagTowrite.append(">"+'\n');
> > > > > outputFileWriter.write(elemTagTowrite.toString
> > ());
> > > > > NodeList childElements = elem.getChildNodes();
> > > > > int noOfchildElements = childElements.getLength
> > ();
> > > > > for(int counter=0; counter < noOfchildElements;
> > counter++){
>
> > > > > if(childElements.item
> > (counter).getClass().toString().contains("Element"))
> > > > > {
> > > > > Node childNode =
> > (Element)childElements.item(counter);
> > > > > Element childElement =
> > (Element)childNode;
> > > > > addTagToTheFile(childElement);
> > > > > }
> > > > > }
> > > > > elemTagTowrite.delete(0, elemTagTowrite.length
> > ());
> > > > > elemTagTowrite.append("</" + elem.getTagName() +
> > ">");
> > > > > outputFileWriter.write(elemTagTowrite.toString
> > ());
> > > > > outputFileWriter.write('\n');
> > > > > }
> > > > > }
>
> > > > > On Mar 24, 9:59 pm, "Dexter's Brain" <[EMAIL PROTECTED]> wrote:
> > > > > > Hello Vesin,
>
> > > > > > I can give you the code, but its not a generalized one. It will
> > work
> > > > > > only for my XML file format. I dont think it will help u.
>
> > > > > > On Mar 24, 8:32 pm, "Vesin Du�ko" <[EMAIL PROTECTED]> wrote:
>
> > > > > > > Can you share your code with as?
>
> > > > > > > On 16/03/2008, Dexter's Brain <[EMAIL PROTECTED]> wrote:
>
> > > > > > > > Hi Nitin,
>
> > > > > > > > I think this is not possible with the current SDK. We should
> > wait till
> > > > > > > > some more packages are added to the SDK. Till then, everything
> > has to
> > > > > > > > be done manually.
> > > > > > > > After banging my head for hours, I wrote a method that would
> > convert a
> > > > > > > > DOM document object to an xml file. It wasn't very difficult
> > though.
>
> > > > > > > > Dexter.
>
> > > > > > > --
>
> --
>
> Vesin Duško
> Software Developer at Execom
>
> email:
> [EMAIL PROTECTED]
> [EMAIL PROTECTED]
> [EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---