以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  哪位能给一个用w3c.dom建立一个xml文档的例子程序(java)[求助]  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=9108)


--  作者:chalbert
--  发布时间:8/3/2004 11:18:00 AM

--  哪位能给一个用w3c.dom建立一个xml文档的例子程序(java)[求助]
rt,谢谢!!
--  作者:asdffdsa22
--  发布时间:8/9/2004 2:53:00 PM

--  
package xmlwriter;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;


public class xmlwriter {

public static void  main(String args[]){
try{
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder=factory.newDocumentBuilder();
  Document doc=builder.parse("links.xml");
  doc.normalize();
   //---取得变量----
  String text="Wudong's Homepage";
  String url="www.wudong.com";
  String author="Wudong Liu";
  String discription="A site from Wudong Liu, give u lots of suprise!!!";
  //-------------
  Text textseg;
  Element link=doc.createElement("link");

  Element linktext=doc.createElement("text");
  textseg=doc.createTextNode(text);
  linktext.appendChild(textseg);
  link.appendChild(linktext);

  Element linkurl=doc.createElement("url");
  textseg=doc.createTextNode(url);
  linkurl.appendChild(textseg);
  link.appendChild(linkurl);

  Element linkauthor=doc.createElement("author");
  textseg=doc.createTextNode(author);
  linkauthor.appendChild(textseg);
  link.appendChild(linkauthor);

  java.util.Calendar rightNow = java.util.Calendar.getInstance();
  String day=Integer.toString(rightNow.get(java.util.Calendar.DAY_OF_MONTH));
  String month=Integer.toString(rightNow.get(java.util.Calendar.MONTH));
  String year=Integer.toString(rightNow.get(java.util.Calendar.YEAR));
  Element linkdate=doc.createElement("date");

  Element linkdateday=doc.createElement("day");
  textseg=doc.createTextNode(day);
  linkdateday.appendChild(textseg);

  Element linkdatemonth=doc.createElement("month");
  textseg=doc.createTextNode(month);
  linkdatemonth.appendChild(textseg);


  Element linkdateyear=doc.createElement("year");
  textseg=doc.createTextNode(year);
  linkdateyear.appendChild(textseg);

  linkdate.appendChild(linkdateday);
  linkdate.appendChild(linkdatemonth);
  linkdate.appendChild(linkdateyear);
  link.appendChild(linkdate);

  Element linkdiscription=doc.createElement("description");
  textseg=doc.createTextNode(discription);
  linkdiscription.appendChild(textseg);
  link.appendChild(linkdiscription);

  doc.getDocumentElement().appendChild(link);

  TransformerFactory tFactory =TransformerFactory.newInstance();
  Transformer transformer = tFactory.newTransformer();
  DOMSource source = new DOMSource(doc);
  StreamResult result = new StreamResult(new java.io.File("links.xml"));
  transformer.transform(source, result);
  }catch(Exception e){
    e.printStackTrace();
  }
  }
}


--  作者:tim
--  发布时间:11/15/2004 5:25:00 PM

--  
I got it ,thanks a lot
--  作者:starting
--  发布时间:5/13/2006 1:47:00 AM

--  
没想到睡前看一例子,不错啊~~~
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
7,951.172ms