新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   >>中国XML论坛<<     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论DOM, SAX, XPath等。
    [返回] 中文XML论坛 - 专业的XML技术讨论区XML.ORG.CN讨论区 - XML技术『 DOM/SAX/XPath 』 → [求助]把java中的XML文档树生成XML文档 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 8554 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: [求助]把java中的XML文档树生成XML文档 举报  打印  推荐  IE收藏夹 
       本主题类别: XML文档存取技术(DOM, SAX)    
     linruixin 帅哥哟,离线,有人找我吗?白羊座1990-3-26
      
      
      等级:大一新生
      文章:6
      积分:72
      门派:XML.ORG.CN
      注册:2010/11/7

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给linruixin发送一个短消息 把linruixin加入好友 查看linruixin的个人资料 搜索linruixin在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看linruixin的博客楼主
    发贴心情 [求助]把java中的XML文档树生成XML文档

    import javax.xml.parsers.*;
    import org.xml.sax.*;
    import java.io.*;
    import org.w3c.dom.*;

    public class code10_4{
      static Document document;
      public static void main(String[] args) throws Exception{
        DocumentBuilderFactory  dbf=DocumentBuilderFactory.newInstance();
        DocumentBuilder db=dbf.newDocumentBuilder();
        //建立新的XML文件
        document=db.newDocument();
        //建立根元素
        Element root=(Element)document.createElement("customer");
        document.appendChild(root);
        //新增子元素customerID
        Element newnode=(Element)document.createElement("customerID");
        root.appendChild(newnode);
        //增加元素的内容
        newnode.appendChild(document.createTextNode("ID"));
        // 增加元素的属性
        newnode=(Element)root.getFirstChild();
        newnode.setAttribute("ID","c050013");
        //新增子元素username
        newnode=(Element)document.createElement("username");
        root.appendChild(newnode);
        //增加元素的内容
        newnode.appendChild(document.createTextNode("cheaperget"));
        //新增子元素password
        newnode=(Element)document.createElement("password");
        //新增元素插入在根元素的最后一个孩子前面
        root.insertBefore(newnode,root.getLastChild());
        //新建文字节点
        Node text=document.createTextNode("12345678");
        //插入文字节点到根节点第一个子节点的兄弟节点,即第二子节点
        Node temp=root.getFirstChild();
        temp.getNextSibling().appendChild(text);
        //显示XML文件
        System.out.println("根元素:"+root.getNodeName());
        //获取根元素的所有子节点
        NodeList childs=root.getChildNodes();
        for(int i=0;i<childs.getLength();i++)
        {
          //显示元素的名字和元素的内容(文字节点)
          System.out.print("元素:"+childs.item(i).getNodeName());
          System.out.println("/"+childs.item(i).getFirstChild().getNodeValue());
          //显示元素的的属性值
          if(childs.item(i).hasAttributes())
          {
            //取属性列表
            NamedNodeMap atts=childs.item(i).getAttributes();
            //使用for循环获取各属性名称和值
            for(int j=0;j<atts.getLength();j++)
            {
             Node att=atts.item(j);
             System.out.print("--"+att.getNodeName());
             System.out.println("/"+att.getNodeValue());
            }
         }   
       }
      }
    DOMSource domsource=new DOMSource(document);
    File file=new File(code10_4.xml);
    StreamResult xmlresult=new StreamResult(file);
    TransformerFactory factory=TransformerFactory.newInstance();
    Transformer transformer=factory.newTransformer();
    transformer.transform(domsource, xmlresult);
    }    
    这样写不对嘛?


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2010/11/28 12:58:00
     
     sunbird_xwf 美女呀,离线,快来找我吧!
      
      
      等级:大一新生
      文章:0
      积分:59
      门派:XML.ORG.CN
      注册:2005/5/19

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给sunbird_xwf发送一个短消息 把sunbird_xwf加入好友 查看sunbird_xwf的个人资料 搜索sunbird_xwf在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看sunbird_xwf的博客2
    发贴心情 
    程序两个问题
    1、添加包import javax.xml.transform.*;
    import javax.xml.transform.stream.*;
    import javax.xml.transform.dom.*;
    因为后面生成xml文档用到的类在这些包中
    2、在main()方法中添加异常的处理
    修改后的程序如下
    import javax.xml.parsers.*;
    import org.xml.sax.*;
    import java.io.*;
    import org.w3c.dom.*;
    import javax.xml.transform.*;   //修改的部分
    import javax.xml.transform.stream.*;
    import javax.xml.transform.dom.*;
    public class test{
      static Document document;
      public static void main(String[] args) throws Exception{
        DocumentBuilderFactory  dbf=DocumentBuilderFactory.newInstance();
        DocumentBuilder db=dbf.newDocumentBuilder();
        //建立新的XML文件
      document=db.newDocument();
        //建立根元素
        Element root=(Element)document.createElement("customer");
        document.appendChild(root);
        //新增子元素customerID
        Element newnode=(Element)document.createElement("customerID");
        root.appendChild(newnode);
        //增加元素的内容
        newnode.appendChild(document.createTextNode("ID"));
        // 增加元素的属性
        newnode=(Element)root.getFirstChild();
        newnode.setAttribute("ID","c050013");
        //新增子元素username
        newnode=(Element)document.createElement("username");
        root.appendChild(newnode);
        //增加元素的内容
        newnode.appendChild(document.createTextNode("cheaperget"));
        //新增子元素password
        newnode=(Element)document.createElement("password");
        //新增元素插入在根元素的最后一个孩子前面
        root.insertBefore(newnode,root.getLastChild());
        //新建文字节点
        Node text=document.createTextNode("12345678");
        //插入文字节点到根节点第一个子节点的兄弟节点,即第二子节点
        Node temp=root.getFirstChild();
        temp.getNextSibling().appendChild(text);
        //显示XML文件
        System.out.println("根元素:"+root.getNodeName());
        //获取根元素的所有子节点
        NodeList childs=root.getChildNodes();
        for(int i=0;i<childs.getLength();i++)
        {
          //显示元素的名字和元素的内容(文字节点)
          System.out.print("元素:"+childs.item(i).getNodeName());
          System.out.println("/"+childs.item(i).getFirstChild().getNodeValue());
          //显示元素的的属性值
          if(childs.item(i).hasAttributes())
          {
            //取属性列表
            NamedNodeMap atts=childs.item(i).getAttributes();
            //使用for循环获取各属性名称和值
            for(int j=0;j<atts.getLength();j++)
            {
             Node att=atts.item(j);
             System.out.print("--"+att.getNodeName());
             System.out.println("/"+att.getNodeValue());
            }
         }   
       }
    try{         //修改的部分
         DOMSource domsource=new DOMSource(document);
         File file=new File("code10_4.xml");
         StreamResult xmlresult=new StreamResult(file);
         TransformerFactory factory=TransformerFactory.newInstance();
         Transformer transformer=factory.newTransformer();
         transformer.transform(domsource,xmlresult);
        }
       catch(Exception e)
         {
           System.out.println(e);
         }
        }
    }
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2010/12/2 14:13:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 DOM/SAX/XPath 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/4/29 16:56:51

    本主题贴数2,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    6,582.031ms