Olá pessoal!
Eu fiz essa classe abaixo para gerar Xml, porém ela apenas aparece no console, como faço para salvar .xml e se existe uma forma mais fácil de gerar Xml...
/*
* GerarXml.java
*
* Created on 5 de Outubro de 2006, 14:10
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package xml;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.*;
/**
*
* @author ccarvalho
*/
public class GerarXml {
public static void main(String[] args) {
try {
// Find the implementation
DocumentBuilderFactory factory
= DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
// Create the document
DocumentType svgDOCTYPE = impl.createDocumentType(
"svg", "-//W3C//DTD SVG 1.0//EN",
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"
);
Document doc = impl.createDocument(
"http://www.w3.org/2000/svg", "svg", svgDOCTYPE);
// Fill the document
Node rootElement = doc.getDocumentElement();
ProcessingInstruction xmlstylesheet
= doc.createProcessingInstruction("xml-stylesheet",
"type=\"text/css\" href=\"standard.css\"");
Comment comment = doc.createComment(
"An example from Chapter 10 of Processing XML with Java");
doc.insertBefore(comment, rootElement);
doc.insertBefore(xmlstylesheet, rootElement);
Node desc = doc.createElementNS(
"http://www.w3.org/2000/svg", "desc");
rootElement.appendChild(desc);
Text descText = doc.createTextNode(
"An example from Processing XML with Java");
desc.appendChild(descText);
Node nome = doc.createElementNS(
"http://www.w3.org/2000/svg", "nome");
rootElement.appendChild(nome);
Text nomeText = doc.createTextNode(
"Christielen");
nome.appendChild(nomeText);
// Serialize the document onto System.out
TransformerFactory xformFactory
= TransformerFactory.newInstance();
Transformer idTransform = xformFactory.newTransformer();
Source input = new DOMSource(doc);
Result output = new StreamResult(System.out);
idTransform.transform(input, output);
}
catch (FactoryConfigurationError e) {
System.out.println("Could not locate a JAXP factory class");
}
catch (ParserConfigurationException e) {
System.out.println(
"Could not locate a JAXP DocumentBuilder class"
);
}
catch (DOMException e) {
System.err.println(e);
}
catch (TransformerConfigurationException e) {
System.err.println(e);
}
catch (TransformerException e) {
System.err.println(e);
}
}
}
