Hi Christians,

Yes, there is another way, you could use the serializer here is an example:


import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.apache.xerces.parsers.DOMParser; import org.xml.sax.SAXException; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.Serializer; import org.apache.xml.serialize.SerializerFactory; import org.apache.xml.serialize.XMLSerializer; import java.io.IOException; import java.io.StringWriter;

public class test {
/**
* This example uses the Serializer package to generate String objects from Elements.
*
* @param argv
* @author Jeffrey Rodriguez
*/
public static void main( String argv[] ) {
DOMParser parser = new DOMParser();
try {
parser.parse( argv[0] );


Document doc = parser.getDocument();
NodeList nodLst = doc.getElementsByTagName("Node3" );//Get all the Node3 elements in a nodeList


OutputFormat format = new OutputFormat( doc );
format.setOmitXMLDeclaration(true);//We need this so we don't generate XML Decl


Node nod;
for (int i = 0; i < nodLst.getLength(); i++) {
StringWriter stringOut = new StringWriter();
XMLSerializer serial = new XMLSerializer( stringOut, format );


               nod = nodLst.item(  i );
               serial.serialize( (Element) nod );
               System.out.println("node = " + stringOut.toString() );
           }

       } catch (SAXException ex) {
           ex.printStackTrace();
       } catch (IOException ex) {
           ex.printStackTrace();
       }

   }

}

Running this against your xml file it will generate:

node = <Node3 attr="Example"/>

Running against the following xml file:

<Node1>
   <Node2>
      <Node3 attr="Example"/>
  </Node2>
   <Node2>
      <Node3 atrr="Test1">This is a test</Node3>
   </Node2>
</Node1>


It would generate:

node = <Node3 attr="Example"/>
node = <Node3 atrr="Test1">This is a test</Node3>

Hope this helps,

                 Jeffrey Rodriguez
                 IBM Silicon Valley - Pervasive Technologies














From: Christians Izquierdo <[EMAIL PROTECTED]>
Reply-To: [EMAIL PROTECTED]
To: [email protected]
Subject: How to get a String with the XML representation of an element
Date: Thu, 8 Feb 2001 21:21:04 -0700

Hi!

Is there any API call to get the String representation of an XML
element?

For example, I have:

Node1
 Node2
 Node3 attr="Example"/
 /Node2
/Node1

I'm using a DOM Parser so I have an object pointing to the element Node2
and I would like to get the following String:
Node3 attr="Example"/

I looked at the DOMWriter but I'm hoping there is another way, it seemed
quite a bit of code for something so simple,

Thanks for your help,
 Christians Izquierdo, BSc. Comp Sci, Internet Services Developer

________________________________________________________________________
_______
 Exceedia, Inc.
  direct tel:780.702.0510 tel:780.413.1521 fax:780.413.0474

  Exceedia - the single authoritative source of
  operational data for your enterprise and partners
 "Exceedia... Be the first to know."

 www.Exceedia.com


_________________________________________________________________ Get your FREE download of MSN Explorer at http://explorer.msn.com



Reply via email to