展开全部
不建议对string拼接xml串容易出错,java 有很多bean转xml的工具,这样容易很多,也不容易出错,JAXB 是不错的选择,j2ee中自带了,或者从搜索引擎中找 ,代码参考,一个TRequest 类,他有几个属性 name,id等等
@XmlRootElement(name = "TRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class TRequest implements java.io.Serializable{
@XmlElement(name="name")
private String name;
@XmlElement(name="id")
private String id;
public String getName() {
return name;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
}
TRequest t= new TRequest();
t.setName("ddddddd");
t.setId("11");
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(t.getClass());
Marshaller marshaller = context.createMarshaller();
// marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
marshaller.marshal(t, writer);
String res=writer.toString();
writer.close();
@XmlRootElement(name = "TRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class TRequest implements java.io.Serializable{
@XmlElement(name="name")
private String name;
@XmlElement(name="id")
private String id;
public String getName() {
return name;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
}
TRequest t= new TRequest();
t.setName("ddddddd");
t.setId("11");
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(t.getClass());
Marshaller marshaller = context.createMarshaller();
// marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
marshaller.marshal(t, writer);
String res=writer.toString();
writer.close();
展开全部
你的意思应该是生成xml文件吧,我使用的是dom4j生成的,例子如下:
package com.test;
import java.io.File;
import java.io.FileWriter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
public class createXml {
/**
* @param args
*/
public static void main(String[] args) {
createXMLFile();
}
public static void createXMLFile() {
Document document = DocumentHelper.createDocument();
Element booksElement = document.addElement("books");
booksElement.addComment("This is a test for dom4j, holen, 2004.9.11");
Element bookElement = booksElement.addElement("book");
bookElement.addAttribute("show", "yes");
Element titleElement = bookElement.addElement("title");
titleElement.setText("Dom4j Tutorials");
bookElement = booksElement.addElement("book");
bookElement.addAttribute("show", "yes");
titleElement = bookElement.addElement("title");
titleElement.setText("Lucene Studing");
bookElement = booksElement.addElement("book");
bookElement.addAttribute("show", "no");
titleElement = bookElement.addElement("title");
titleElement.setText("Lucene in Action");
Element ownerElement = booksElement.addElement("owner");
ownerElement.setText("O'Reilly");
try {
XMLWriter writer = new XMLWriter(new FileWriter(new File(
"e://jwp.xml")));
writer.write(document);
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
package com.test;
import java.io.File;
import java.io.FileWriter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
public class createXml {
/**
* @param args
*/
public static void main(String[] args) {
createXMLFile();
}
public static void createXMLFile() {
Document document = DocumentHelper.createDocument();
Element booksElement = document.addElement("books");
booksElement.addComment("This is a test for dom4j, holen, 2004.9.11");
Element bookElement = booksElement.addElement("book");
bookElement.addAttribute("show", "yes");
Element titleElement = bookElement.addElement("title");
titleElement.setText("Dom4j Tutorials");
bookElement = booksElement.addElement("book");
bookElement.addAttribute("show", "yes");
titleElement = bookElement.addElement("title");
titleElement.setText("Lucene Studing");
bookElement = booksElement.addElement("book");
bookElement.addAttribute("show", "no");
titleElement = bookElement.addElement("title");
titleElement.setText("Lucene in Action");
Element ownerElement = booksElement.addElement("owner");
ownerElement.setText("O'Reilly");
try {
XMLWriter writer = new XMLWriter(new FileWriter(new File(
"e://jwp.xml")));
writer.write(document);
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这问题没法回答。。。。。。。。看你源数据是什么样子的,并且想要什么样子的xml格式。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询