生成XML的问题 我记得用JAXP不能满足你的要求,用DOM吧。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考JAXP的JAVADOC,有这样的例子 呵呵,当然可以了,你要求保存文件吧?算了,贴你一个class好了,你可以参照一下。(不过这只是我的一各demo)package utility;import javax.xml.transform.stream.*;import javax.xml.transform.dom.*;import javax.xml.parsers.*;import org.xml.sax.*;import org.xml.sax.helpers.*;import org.w3c.dom.*;import javax.xml.transform.*;import java.io.*;/** * Insert the type's description here. * Creation date: (2001-8-31 15:21:04) * @author: Administrator */public class XmlManager { private static org.w3c.dom.Document document;/** * XmlManager constructor comment. */public XmlManager(){ super(); fileName = "map.xml"; loadFile(fileName);}/** * Insert the method's description here. * Creation date: (2001-9-1 11:49:29) */public void addMethod(){ try { Element cityElem = document.createElement("AllElement"); Element childElem = document.createElement("Element"); childElem.appendChild(document.createTextNode("TextNode")); cityElem.appendChild(childElem); NodeList nodeList = document.getElementsByTagName("city"); nodeList.item(0).appendChild(cityElem); saveFile(document, "map.xml"); } catch (Exception e) { System.out.println(e); }}/** * Insert the method's description here. * Creation date: (2001-9-1 9:58:46) * @return org.w3c.dom.Document */public static org.w3c.dom.Document getDocument() { return document; }/** * Insert the method's description here. * Creation date: (2001-8-31 16:07:12) * @return int */public int getInfo(){ NodeList nodeList = document.getElementsByTagName("city"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); NodeList nodeChildList = node.getChildNodes(); for (int j = 0; j < nodeChildList.getLength(); j++) { Node nodeChild = nodeChildList.item(j); if (nodeChild.getNodeType() == Node.ELEMENT_NODE) { // System.out.println(""); // System.out.println("ELEMENT_NODE: ======================= Start"); DebugTool.traceMessage("Name : " + nodeChild.getNodeName()); Node nodeChildText = nodeChild.getChildNodes().item(0); if (nodeChildText.getNodeType() == Node.TEXT_NODE) { DebugTool.traceMessage("ChildValue : " + nodeChildText.getNodeValue()); } // System.out.println("ELEMENT_NODE: ======================= End"); } } } return 0;}/** * Insert the method's description here. * Creation date: (2001-8-31 15:49:26) * @return int * @param fileName java.lang.String */public int loadFile(String fileName){ if (true) { // } try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(fileName); } catch (SAXException sxe) { // Error generated during parsing) Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace(); } catch (ParserConfigurationException pce) { // Parser with specified options can't be built pce.printStackTrace(); } catch (IOException ioe) { // I/O error ioe.printStackTrace(); } return 0;}/** * Insert the method's description here. * Creation date: (2001-8-31 16:19:45) * @param args java.lang.String[] */public static void main(String[] args){ XmlManager fileManager = new XmlManager(); fileManager.loadFile("map.xml"); // fileManager.getInfo(); // fileManager.modMethod();}/** * Insert the method's description here. * Creation date: (2001-9-1 11:49:29) */public void modMethod(){ try { Element cityElem = document.createElement("AllElement"); Element childElem = document.createElement("Element"); childElem.appendChild(document.createTextNode("TextNode")); cityElem.appendChild(childElem); NodeList nodeList = document.getElementsByTagName("city"); nodeList.item(0).appendChild(cityElem); saveFile(document, "map.xml"); } catch (Exception e) { System.out.println(e); }}/** * Insert the method's description here. * Creation date: (2001-9-3 10:44:11) * @param document org.w3c.dom.Document * @param fileName java.lang.String */public void saveFile(Document doc, String fileName){ try { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer tf = tranFactory.newTransformer(); DOMSource dom = new DOMSource(doc); java.util.Properties properties = tf.getOutputProperties(); properties.setProperty(OutputKeys.ENCODING, "GBK"); tf.setOutputProperties(properties); OutputStreamWriter osw = new OutputStreamWriter( new BufferedOutputStream(new FileOutputStream(fileName)), "GBK"); StreamResult sr = new StreamResult(osw); tf.transform(dom, sr); } catch (Exception e) { System.out.println(e); }}/** * Insert the method's description here. * Creation date: (2001-9-1 9:58:46) * @param newDocument org.w3c.dom.Document */public static void setDocument(org.w3c.dom.Document newDocument) { document = newDocument;} private java.lang.String fileName;/** * Insert the method's description here. * Creation date: (2001-9-3 10:44:11) * @param document org.w3c.dom.Document * @param fileName java.lang.String */public void saveFile(){ saveFile(document, fileName);}} 说明:我的XML DOM是在程序中生成的空的DOM, 要从数据库读数据来填充的. ssh批量添加问题,听祖先说,标题要长,大哥大姐才会进来 求助:SESSION问题? 多图片上传 银行缴费问题 java 泛型 6000万条IP地址数据里面有部分重复,如何把重复的部分找到并写出相应的所属区域 struts2 问题 请教.Hibernate 中多主键关联的 hbm 文件如何写 100分请高手看看Tomcat启动示显示如下 求教关于SQL Server 2000 JDBC驱动 哪位大侠能讲讲怎么将一个用EJB写的站点做成安装程序? 简单问题!!!XML问题!!
算了,贴你一个class好了,你可以参照一下。(不过这只是我的一各demo)
package utility;import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import java.io.*;/**
* Insert the type's description here.
* Creation date: (2001-8-31 15:21:04)
* @author: Administrator
*/public class XmlManager {
private static org.w3c.dom.Document document;
/**
* XmlManager constructor comment.
*/
public XmlManager()
{
super();
fileName = "map.xml";
loadFile(fileName);
}
/**
* Insert the method's description here.
* Creation date: (2001-9-1 11:49:29)
*/
public void addMethod()
{
try
{
Element cityElem = document.createElement("AllElement");
Element childElem = document.createElement("Element");
childElem.appendChild(document.createTextNode("TextNode"));
cityElem.appendChild(childElem); NodeList nodeList = document.getElementsByTagName("city");
nodeList.item(0).appendChild(cityElem);
saveFile(document, "map.xml");
}
catch (Exception e)
{
System.out.println(e);
}}
/**
* Insert the method's description here.
* Creation date: (2001-9-1 9:58:46)
* @return org.w3c.dom.Document
*/
public static org.w3c.dom.Document getDocument() {
return document;
}
/**
* Insert the method's description here.
* Creation date: (2001-8-31 16:07:12)
* @return int
*/
public int getInfo()
{
NodeList nodeList = document.getElementsByTagName("city");
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
NodeList nodeChildList = node.getChildNodes();
for (int j = 0; j < nodeChildList.getLength(); j++)
{
Node nodeChild = nodeChildList.item(j);
if (nodeChild.getNodeType() == Node.ELEMENT_NODE)
{
// System.out.println("");
// System.out.println("ELEMENT_NODE: ======================= Start");
DebugTool.traceMessage("Name : " + nodeChild.getNodeName());
Node nodeChildText = nodeChild.getChildNodes().item(0);
if (nodeChildText.getNodeType() == Node.TEXT_NODE)
{
DebugTool.traceMessage("ChildValue : " + nodeChildText.getNodeValue());
}
// System.out.println("ELEMENT_NODE: ======================= End");
}
}
}
return 0;
}
/**
* Insert the method's description here.
* Creation date: (2001-8-31 15:49:26)
* @return int
* @param fileName java.lang.String
*/
public int loadFile(String fileName)
{
if (true)
{
//
}
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(fileName);
}
catch (SAXException sxe)
{
// Error generated during parsing)
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
}
catch (ParserConfigurationException pce)
{
// Parser with specified options can't be built
pce.printStackTrace();
}
catch (IOException ioe)
{
// I/O error
ioe.printStackTrace();
}
return 0;
}
/**
* Insert the method's description here.
* Creation date: (2001-8-31 16:19:45)
* @param args java.lang.String[]
*/
public static void main(String[] args)
{
XmlManager fileManager = new XmlManager();
fileManager.loadFile("map.xml");
// fileManager.getInfo();
// fileManager.modMethod();}
/**
* Insert the method's description here.
* Creation date: (2001-9-1 11:49:29)
*/
public void modMethod()
{
try
{
Element cityElem = document.createElement("AllElement");
Element childElem = document.createElement("Element");
childElem.appendChild(document.createTextNode("TextNode"));
cityElem.appendChild(childElem); NodeList nodeList = document.getElementsByTagName("city");
nodeList.item(0).appendChild(cityElem);
saveFile(document, "map.xml");
}
catch (Exception e)
{
System.out.println(e);
}}
/**
* Insert the method's description here.
* Creation date: (2001-9-3 10:44:11)
* @param document org.w3c.dom.Document
* @param fileName java.lang.String
*/
public void saveFile(Document doc, String fileName)
{
try
{
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer tf = tranFactory.newTransformer();
DOMSource dom = new DOMSource(doc); java.util.Properties properties = tf.getOutputProperties();
properties.setProperty(OutputKeys.ENCODING, "GBK");
tf.setOutputProperties(properties); OutputStreamWriter osw =
new OutputStreamWriter(
new BufferedOutputStream(new FileOutputStream(fileName)),
"GBK");
StreamResult sr = new StreamResult(osw);
tf.transform(dom, sr);
}
catch (Exception e)
{
System.out.println(e);
}
}
/**
* Insert the method's description here.
* Creation date: (2001-9-1 9:58:46)
* @param newDocument org.w3c.dom.Document
*/
public static void setDocument(org.w3c.dom.Document newDocument) {
document = newDocument;
}
private java.lang.String fileName;/**
* Insert the method's description here.
* Creation date: (2001-9-3 10:44:11)
* @param document org.w3c.dom.Document
* @param fileName java.lang.String
*/
public void saveFile()
{
saveFile(document, fileName);
}}
我的XML DOM是在程序中生成的空的DOM, 要从数据库读数据来填充的.