java的JDOM能否生成这样的XML文件格式
我想生成这样的格式<?xmlversion="1.0"encoding="UTF-8"?><weavertype="pushrequest"><userid>1</use...
我想生成这样的格式
<?xml version="1.0" encoding="UTF-8"?>
<weaver type="pushrequest">
<userid>1</userid>
<username>系统管理员</username>
<datas>
<data>
<name>姓名</name>
<mail>邮箱地址</mail>
<qq>QQ号码</qq>
</data>
<data>
<name>姓名</name>
<mail>邮箱地址</mail>
<qq>QQ号码</qq>
</data>
</datas>
</weaver>
请问代码如何实现,用JDOM包实现。
如果可以 另外加分!
本人正在学习这方面,如有高手请赐教
本人QQ 281081050
能给出具体的列子吗 展开
<?xml version="1.0" encoding="UTF-8"?>
<weaver type="pushrequest">
<userid>1</userid>
<username>系统管理员</username>
<datas>
<data>
<name>姓名</name>
<mail>邮箱地址</mail>
<qq>QQ号码</qq>
</data>
<data>
<name>姓名</name>
<mail>邮箱地址</mail>
<qq>QQ号码</qq>
</data>
</datas>
</weaver>
请问代码如何实现,用JDOM包实现。
如果可以 另外加分!
本人正在学习这方面,如有高手请赐教
本人QQ 281081050
能给出具体的列子吗 展开
展开全部
写个简单例子,可以生成你需要的xml,如果想变为动态数据,请参考此例。记得要加入该有jar文件
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
public class JDOM {
private static Element getElementForAttr(String name,String[] keyfield,String[] key){
Element item=new Element(name);
if(keyfield!=null&&keyfield.length>0){
for(int i=0;i<keyfield.length;i++){
item.setAttribute(keyfield[i],key[i]);
}
}
return item;
}
/*
* 输出到指定的xml文件
*/
public static long WriteXMLDoc(Document Doc,String filepath,String filename){
try{
org.jdom.output.Format format = org.jdom.output.Format.getCompactFormat();
format.setEncoding("UTF-8");
format.setIndent(" "); //缩进4个空格后换行
XMLOutputter XMLOut = new XMLOutputter(format);
// 输出 XML 文件;
File file=new File(filepath);
boolean s=false;
if(file.exists()==false){
s=file.mkdirs();
}
FileOutputStream fops= new FileOutputStream(filepath+filename);
XMLOut.output(Doc, fops);
fops.close();
return 0;
}catch(Exception e){
e.printStackTrace();
return -1;
}
}
public static void main(String[] args){
Element root = new Element("weaver");
root.setAttribute("type","pushrequest");// 设置根节点属性
Document Doc = new Document(root);
List xmlrecords = root.getChildren();
xmlrecords.add(getElementForAttr("userid",null,null).addContent("1"));
xmlrecords.add(getElementForAttr("username",null,null).addContent("系统管理员"));
Element datas=getElementForAttr("datas",null,null);
Element data=null;
Element name=null;
Element mail=null;
Element qq=null;
for(int i=0;i<2;i++){//添加data子节点
data=getElementForAttr("data",null,null);
name=getElementForAttr("name",null,null).addContent("姓名");
mail=getElementForAttr("mail",null,null).addContent("邮箱地址");
qq=getElementForAttr("qq",null,null).addContent("QQ号码");
data.addContent(name);
data.addContent(mail);
data.addContent(qq);
datas.addContent(data);//添加datas一个子节点data
}
xmlrecords.add(datas);
WriteXMLDoc(Doc,"c://","test.xml");
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
public class JDOM {
private static Element getElementForAttr(String name,String[] keyfield,String[] key){
Element item=new Element(name);
if(keyfield!=null&&keyfield.length>0){
for(int i=0;i<keyfield.length;i++){
item.setAttribute(keyfield[i],key[i]);
}
}
return item;
}
/*
* 输出到指定的xml文件
*/
public static long WriteXMLDoc(Document Doc,String filepath,String filename){
try{
org.jdom.output.Format format = org.jdom.output.Format.getCompactFormat();
format.setEncoding("UTF-8");
format.setIndent(" "); //缩进4个空格后换行
XMLOutputter XMLOut = new XMLOutputter(format);
// 输出 XML 文件;
File file=new File(filepath);
boolean s=false;
if(file.exists()==false){
s=file.mkdirs();
}
FileOutputStream fops= new FileOutputStream(filepath+filename);
XMLOut.output(Doc, fops);
fops.close();
return 0;
}catch(Exception e){
e.printStackTrace();
return -1;
}
}
public static void main(String[] args){
Element root = new Element("weaver");
root.setAttribute("type","pushrequest");// 设置根节点属性
Document Doc = new Document(root);
List xmlrecords = root.getChildren();
xmlrecords.add(getElementForAttr("userid",null,null).addContent("1"));
xmlrecords.add(getElementForAttr("username",null,null).addContent("系统管理员"));
Element datas=getElementForAttr("datas",null,null);
Element data=null;
Element name=null;
Element mail=null;
Element qq=null;
for(int i=0;i<2;i++){//添加data子节点
data=getElementForAttr("data",null,null);
name=getElementForAttr("name",null,null).addContent("姓名");
mail=getElementForAttr("mail",null,null).addContent("邮箱地址");
qq=getElementForAttr("qq",null,null).addContent("QQ号码");
data.addContent(name);
data.addContent(mail);
data.addContent(qq);
datas.addContent(data);//添加datas一个子节点data
}
xmlrecords.add(datas);
WriteXMLDoc(Doc,"c://","test.xml");
}
}
展开全部
验证过 复制过去直接可以用
package Dom;
import java.io.FileWriter;
import java.io.Writer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Text {
public static void main(String[] args)
throws Exception{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder bf = dbf.newDocumentBuilder();
Document doc = bf.newDocument();
Element cat = doc.createElement("datas");
Element cd = doc.createElement("data");
Element name = doc.createElement("name");//
Element email = doc.createElement("email");
Element qq = doc.createElement("qq");
Element name1 = doc.createElement("姓名");
Element email1 = doc.createElement("邮箱");
Element qq1 = doc.createElement("qq");
doc.appendChild(cat);
cd.appendChild(doc.createTextNode("测试源码"));
cat.appendChild(cd);
cd.appendChild(name);
cd.appendChild(email);
cd.appendChild(qq);
name.appendChild(name1);
email.appendChild(email1);
qq.appendChild(qq1);
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
Writer out = new FileWriter("c:\\name.xml");
tf.transform(new DOMSource(doc),new StreamResult(out));
out.close();
}
}
package Dom;
import java.io.FileWriter;
import java.io.Writer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Text {
public static void main(String[] args)
throws Exception{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder bf = dbf.newDocumentBuilder();
Document doc = bf.newDocument();
Element cat = doc.createElement("datas");
Element cd = doc.createElement("data");
Element name = doc.createElement("name");//
Element email = doc.createElement("email");
Element qq = doc.createElement("qq");
Element name1 = doc.createElement("姓名");
Element email1 = doc.createElement("邮箱");
Element qq1 = doc.createElement("qq");
doc.appendChild(cat);
cd.appendChild(doc.createTextNode("测试源码"));
cat.appendChild(cd);
cd.appendChild(name);
cd.appendChild(email);
cd.appendChild(qq);
name.appendChild(name1);
email.appendChild(email1);
qq.appendChild(qq1);
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
Writer out = new FileWriter("c:\\name.xml");
tf.transform(new DOMSource(doc),new StreamResult(out));
out.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询