how to create new XML document with DOM API of JAVA

hi all, please tell me how to create a new XML document through JAVA with DOM API. please give me full information and coding for this. thanks & regards Amit Bansal
[204 byte] By [bansal_amit] at [2007-9-26 2:19:06]
# 1

Using which parser; Oracle's, Sun's, IBM's, they are all different. If you read the doc, its says the W3C never agreed upon a standard.

Also, do you just want info on how to create the doc or how to add elements....

Brunky

Oracle's is essentially

XMLDocument doc = new XMLDocument();

doc.setVersion("1.0");

XMLElement top = new XMLElement("MyDocument");

doc.appendChild(top);

This should yield

<?xml version="1.0"?>

<MyDocument/>

Brunky at 2007-6-29 9:21:00 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

I have problem using setNodeValue using DOM API

Do you have an example that works?

My code is the following:

package com.odafs.documentViewer.client.controler;

import com.odafs.mirage.businessComponent.mailDistribution.*;

import com.odafs.mirage.businessComponent.mailDistribution.documentAS400.*;

import java.io.*;

import java.util.*;

import javax.xml.transform.*;

import javax.xml.transform.dom.*;

import javax.xml.transform.stream.*;

import org.apache.batik.dom.svg.*;

import org.w3c.dom.*;

import org.w3c.dom.svg.*;

import com.odafs.util.log.*;

public class TestDom

{

static public void main(String[] args)

{

String svgNS;

Document domSvg;

DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();

svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;

domSvg = impl.createDocument(svgNS, "svg", null);

// get the root element (the svg element)

Element svgRoot = domSvg.getDocumentElement();

svgRoot.setAttributeNS(null, "width", "500");

svgRoot.setAttributeNS(null, "height", "600");

// create the rectangle

Element rectangle = domSvg.createElementNS(svgNS, "rect");

rectangle.setAttributeNS(null, "x", "10");

rectangle.setAttributeNS(null, "y", "20");

rectangle.setAttributeNS(null, "width", "100");

rectangle.setAttributeNS(null, "height", "50");

rectangle.setAttributeNS(null, "style", "fill:red");

svgRoot.appendChild(rectangle);

// create the text

Element textElement = domSvg.createElementNS(svgNS, "text");

textElement.setAttributeNS(null, "x", "10");

textElement.setAttributeNS(null, "y", "20");

// ////////////////////MYPROBLEM //////////////////////

// the following method 'setNodeValue' dowsn't work, dowsn't add a value to the node

textElement.setNodeValue("generateSVG");

// Why? Is there something wrong?

// /////////////////////////////////////////////////////////////////////////////

svgRoot.appendChild(textElement);

if (domSvg != null)

{

System.out.println("non null");

try {

Transformer transf = TransformerFactory.newInstance().newTransformer();

DOMSource source = new DOMSource(domSvg);

StreamResult result = new StreamResult("DOM.svg");

transf.transform(source, result);

System.out.println("ok!");

}

catch(TransformerConfigurationException e) {

System.out.println("generation error");

}

catch(TransformerException e) {

System.out.println("Transformation error");

}

}

}

}

Thanks a lot in advance!!!

Ciao!

christophe.guillaume at 2007-6-29 9:21:00 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...