2009年4月16日 星期四

[Dom4j] 產生xml


Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("Message");
root.addElement("Date").addText("");

int i = 1;
int j = 0;
Set<object> set = null;
for(object p : list){

Element items = root.addElement("item");
items.addAttribute("id", String.valueOf(i));
items.addElement("PID").addText("");
items.addElement("PTitle").addText("");
items.addElement("Depict").addText("");
items.addElement("Type").addText("");
items.addElement("Num").addText("");

set = p.getxxx();
j = 1;
for(object a : set){
Element content = items.addElement("Content");
content.addAttribute("id", String.valueOf(j));
content.addElement("Title").addText("");
content.addElement("Desc").addText("");
j++;
}
i++;
}

System.out.println(doc.asXML());

顯示結果為
<Message>
 <Date> <Item id=1>
  <PID>  <PTitle>  <Depict>  <Type>  <Num>2  <Content id =1>
   <Title>   <Desc>  </Content>
  <Content id =2 >
   <Title>   <Desc>  </Content>
 </Item>
</Message>

沒有留言: