我想得到如下这样的xml:<?xml version="1.0" encoding="GB2312"?>
<root>
<mode name="a1">
<pro name="proa1">
<name>producta1</name>
<times>2005/1/1</times>
</pro>
<pro name="proa2">
<name>producta2</name>
<times>2005/1/2</times>
</pro>
</mode> <mode name="b1">
<pro name="prob1">
<name>productb1</name>
<times>2005/1/1</times>
</pro>
<pro name="prob2">
<name>productb2</name>
<times>2005/1/2</times>
</pro>
</mode>
</root>
我编写的java源码如下,大家看看我的结构,里面结点内容(var1,var2,var3,var4)是变量:…………
Document doc = null;
doc = db.newDocument();Element root = doc.createElement("root");
doc.appendChild(root);for(int i=0;i<4;i++)
{ Element modelroot = doc.createElement("mode");
modelroot.setAttribute("name", var1)
root.appendChild(modelroot);
Element proroot = doc.createElement("pro");
proroot.setAttribute("name",var2);
modelroot.appendChild(proroot); Element nameroot = doc.createElement("name");
proroot.appendChild(nameroot);
Text price1Num = doc.createTextNode(var3);
nameroot.appendChild(price1Num); Element timesroot = doc.createElement("times");
proroot.appendChild(timesroot);
Text price2Num = doc.createTextNode(var4);
timesroot.appendChild(price2Num);
}
…………结果生成的xml确是如下的内容,达不到我想要的xml内容:
<?xml version="1.0" encoding="GB2312"?>
<root>
<mode name="a1">
<pro name="proa1">
<name>producta1</name>
<times>2005/1/1</times>
</pro>
</mode>
<mode name="a1">
<pro name="proa2">
<name>producta2</name>
<times>2005/1/2</times>
</pro>
</mode> <mode name="b1">
<pro name="prob1">
<name>productb1</name>
<times>2005/1/1</times>
</pro>
</mode>
<mode name="b1">
<pro name="prob2">
<name>productb2</name>
<times>2005/1/2</times>
</pro>
</mode>
</root>为什么会输出上面的情况?
有什么办法可以解决吗?大家帮我改一下代码
<root>
<mode name="a1">
<pro name="proa1">
<name>producta1</name>
<times>2005/1/1</times>
</pro>
<pro name="proa2">
<name>producta2</name>
<times>2005/1/2</times>
</pro>
</mode> <mode name="b1">
<pro name="prob1">
<name>productb1</name>
<times>2005/1/1</times>
</pro>
<pro name="prob2">
<name>productb2</name>
<times>2005/1/2</times>
</pro>
</mode>
</root>
我编写的java源码如下,大家看看我的结构,里面结点内容(var1,var2,var3,var4)是变量:…………
Document doc = null;
doc = db.newDocument();Element root = doc.createElement("root");
doc.appendChild(root);for(int i=0;i<4;i++)
{ Element modelroot = doc.createElement("mode");
modelroot.setAttribute("name", var1)
root.appendChild(modelroot);
Element proroot = doc.createElement("pro");
proroot.setAttribute("name",var2);
modelroot.appendChild(proroot); Element nameroot = doc.createElement("name");
proroot.appendChild(nameroot);
Text price1Num = doc.createTextNode(var3);
nameroot.appendChild(price1Num); Element timesroot = doc.createElement("times");
proroot.appendChild(timesroot);
Text price2Num = doc.createTextNode(var4);
timesroot.appendChild(price2Num);
}
…………结果生成的xml确是如下的内容,达不到我想要的xml内容:
<?xml version="1.0" encoding="GB2312"?>
<root>
<mode name="a1">
<pro name="proa1">
<name>producta1</name>
<times>2005/1/1</times>
</pro>
</mode>
<mode name="a1">
<pro name="proa2">
<name>producta2</name>
<times>2005/1/2</times>
</pro>
</mode> <mode name="b1">
<pro name="prob1">
<name>productb1</name>
<times>2005/1/1</times>
</pro>
</mode>
<mode name="b1">
<pro name="prob2">
<name>productb2</name>
<times>2005/1/2</times>
</pro>
</mode>
</root>为什么会输出上面的情况?
有什么办法可以解决吗?大家帮我改一下代码
doc = db.newDocument();Element root = doc.createElement("root");
doc.appendChild(root);Element modelroot = doc.createElement("mode");
modelroot.setAttribute("name", var1)
root.appendChild(modelroot);
for(int i=0;i<4;i++)
{Element proroot = modelroot .createElement("pro");
proroot.setAttribute("name",var2);
modelroot.appendChild(proroot);Element nameroot = proroot .createElement("name");
proroot.appendChild(nameroot);
Text price1Num = doc.createTextNode(var3);
nameroot.appendChild(price1Num);Element timesroot = proroot .createElement("times");
proroot.appendChild(timesroot);
Text price2Num = doc.createTextNode(var4);
timesroot.appendChild(price2Num);}
…………
//nameroot 是proroot 的孩子
Document doc = null;
doc = db.newDocument();Element root = doc.createElement("root");
doc.appendChild(root);for(int i=0;i<4;i++)
{
Element modelroot = doc.createElement("mode");
modelroot.setAttribute("name", var1)
root.appendChild(modelroot);Element proroot = modelroot .createElement("pro");
proroot.setAttribute("name",var2);
modelroot.appendChild(proroot);Element nameroot = proroot .createElement("name");
proroot.appendChild(nameroot);
Text price1Num = doc.createTextNode(var3);
nameroot.appendChild(price1Num);Element timesroot = proroot .createElement("times");
proroot.appendChild(timesroot);
Text price2Num = doc.createTextNode(var4);
timesroot.appendChild(price2Num);}
…………
这样看看,我用的是dom4j,你用的什么解释器?
<root>
<mode name="a1">
<pro name="proa1">
<name>producta1</name>
<times>2005/1/1</times>
</pro>
</mode>
<mode name="a1">
<pro name="proa2">
<name>producta2</name>
<times>2005/1/2</times>
</pro>
</mode> <mode name="b1">
<pro name="prob1">
<name>productb1</name>
<times>2005/1/1</times>
</pro>
</mode>
<mode name="b1">
<pro name="prob2">
<name>productb2</name>
<times>2005/1/2</times>
</pro>
</mode>
</root>
可是我要的确是如下的结构树啊(合并了父类相同的子类,使它们在一个根结点之上)<?xml version="1.0" encoding="GB2312"?>
<root>
<mode name="a1">
<pro name="proa1">
<name>producta1</name>
<times>2005/1/1</times>
</pro>
<pro name="proa2">
<name>producta2</name>
<times>2005/1/2</times>
</pro>
</mode> <mode name="b1">
<pro name="prob1">
<name>productb1</name>
<times>2005/1/1</times>
</pro>
<pro name="prob2">
<name>productb2</name>
<times>2005/1/2</times>
</pro>
</mode>
</root>有什么办法吗?????
有相同名字的就不创建直接在那个mode下增加pro,
如果没有相同名字的就建立新的mode
root.selectNodes("mode")
/*
* MakeXML2.java
*
* Created on 2005年11月4日, 上午 14:52
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
import java.io.*;
import org.jdom.*;
import org.jdom.output.*;/**
*
* @author tao
*/
public class MakeXML2 {
/** Creates a new instance of MakeXML2 */
public MakeXML2() {
}
public void Make(){
Document doc=null;
Element root=null;
root=new Element("root");
doc=new Document(root);
String [] names=new String[]{"name","times"};
String [] datas=new String[]{"共同的家","2005-11-4"};
try{
for(int k = 0;k<5;k++){//make
Element element=new Element("mode");
element.setAttribute("name",""+(k+1));
for(int j = 0;j<2;j++){
Element element2 = new Element("pro");
element2.setAttribute("name",""+(j+1));
//Element elt=new Element("pro");
for(int i=0;i<names.length;i++){
Element el=new Element(names[i]);
el.setText(datas[i]);
element2.addContent(el);
}
element.addContent(element2);
}
root.addContent(element);
}
Format format = Format.getCompactFormat();
format.setEncoding("utf-8");
format.setIndent(" ");
XMLOutputter XMLOut = new XMLOutputter(format);
XMLOut.output(doc, new FileOutputStream("search.xml"));
}catch(Exception e){
}
}
public static void main(String[] args){
MakeXML2 m = new MakeXML2();
m.Make();
}
}