[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: generating an DOM object from a non-XML source in Java
- From: Jurgen <jurgen.moortgat@oracle.com>
- To: "Newman, Todd" <ToddNewman@TDWaterhouse.com>
- Date: Wed, 21 Mar 2001 08:55:53 +0100
hi,
did you already tried Oracle's xml libraries (you can get them at
technet).
your code may look like this:
// Construct the new XML Document.
XMLDocument xmlDoc = new XMLDocument() ;
// Create the root Node.
XMLNode rootNode = (XMLNode) xmlDoc.createElement("ROOT") ;
// Create a new node.
XMLNode node = (XMLNode) xmlDoc.createElement("TAG") ;
// Add text to the new node.
node.appendChild(new XMLText("Value")) ;
// Append a child to the root tag.
rootNode.appendChild(node) ;
/* Repeat the above statements as much as needed (you could make
this recursive in order to create child and/or parent tags.
*/
// Append the complete root node (tree) to the new XMLDocument.
xmlDoc.appendChild(rootNode) ;
cheers,
Jurgen - Oracle Belgium.
"Newman, Todd" wrote:
>
> Hi,
>
> I've got what I hope is a simple question for the pros. I'm trying to loop
> through some delimited text and generate an XML object to end up with a
> valid XML object and/or string.
>
> I've been searching the web and my Visual Age online help for the better
> part of two days with not much to show for myself. It seems to me that this
> should be a common thing to do, but I've not been very successful.
>
> In fact, I've done this several times before in the VB world. My steps in
> that universe look like this:
> (1) Create the DOMDocument
> (2) Load the string "<XML></XML>" into the document
> (3) Get a reference to the Document Element
> (4) Create Nodes from the DOMDocument
> (5) Append the nodes to the Document Element or one of its children
> (6) Repeat 4&5 as required
>
> So I've been trying to do something similar with Java. I'm using the
> libraries which come with IBM Visual Age for Java, and I also downloaded
> Saxon 6.0.2.
>
> First, I tried using com.ibm.xml.dom. With that, I am able to create an
> object of type DocumentImpl like so: (step 1)
> DocumentImpl di = new DocumentImpl();
> Then, I can get a reference to the document element: (step 3)
> ElementImpl elRoot = (ElementImpl)di.getDocumentElement();
> And I can create an Element: (part of step 4)
> NodeImpl node1 = (NodeImpl)di.createElement("T1");
> But I can't set the text of the element using:
> node1.setNodeValue("Todd")
> ...without getting an exception (part of step 4.) I also can't append the
> node (step 5.) And I can't set call setNodeValue on my root element (step
> 2), though I don't know if that's important.
>
> So then, I moved on to the com.icl.saxon.tree library. Here I had less
> success. I could create the DocumentImpl: (step 1)
> DocumentImpl di = new DocumentImpl(1);
> And I could create a node: (step 4?)
> ElementImpl node1 = new ElementImpl();
> But that's as far as I've gotten.
>
> So now I'm stuck and feeling a bit dumb. You might have already figured out
> I'm not quite a novice, but far from experienced with Java. Any direction
> would be appreciated!
>
> thanks,
> Todd
> toddnewman@tdwaterhouse.com
>
> ------------------------------------------------------------------
> The xml-dev list is sponsored by XML.org, an initiative of OASIS
> <http://www.oasis-open.org>
>
> The list archives are at http://lists.xml.org/archives/xml-dev/
>
> To unsubscribe from this elist send a message with the single word
> "unsubscribe" in the body to: xml-dev-request@lists.xml.org