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

    >> 本版讨论DOM, SAX, XPath等。
    [返回] 中文XML论坛 - 专业的XML技术讨论区XML.ORG.CN讨论区 - XML技术『 DOM/SAX/XPath 』 → [原创]Dom解析xml文件中,如何更改节点的值?代码查错,请教高手! 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 4010 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: [原创]Dom解析xml文件中,如何更改节点的值?代码查错,请教高手! 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     simoon1981 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:0
      积分:54
      门派:XML.ORG.CN
      注册:2006/11/13

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给simoon1981发送一个短消息 把simoon1981加入好友 查看simoon1981的个人资料 搜索simoon1981在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看simoon1981的博客楼主
    发贴心情 [原创]Dom解析xml文件中,如何更改节点的值?代码查错,请教高手!

    xml文件如下
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <inventory>
        <book year="2000">
            <title>Snow Crash</title>
            <author>Neal Stephenson</author>
            <publisher>Spectra</publisher>
            <isbn>0553380958</isbn>
            <price>14.95</price>
        </book>
        <book year="2001">
            <title>Java Language</title>
            <author>James Goslin</author>
            <publisher>Spectra</publisher>
            <isbn>0523356428</isbn>
            <price>14.95</price>
        </book>   
        <book year="2003">
            <title>Ajax</title>
            <author>Simon Elvis</author>
            <author>Jerry Pournelle</author>
            <publisher>Pocket</publisher>
            <isbn>0531816460</isbn>
            <price>5.99</price>
        </book>
        <book year="2005">
            <title>Burning Tower</title>
            <author>Larry Niven</author>
            <author>Jerry Pournelle</author>
            <publisher>Pocket</publisher>
            <isbn>0743416910</isbn>
            <price>5.99</price>
        </book>
        <book year="1995">
            <title>Zodiac</title>
            <author>Neal Stephenson</author>
            <publisher>Spectra</publisher>
            <isbn>0553573862</isbn>
            <price>7.50</price>
        </book>
    </inventory>
    代码如下
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.ParserConfigurationException;

    import java.io.File;
    import java.io.IOException;

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NamedNodeMap;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    public class DomBuilder {
     public static void main(String[] args) {
      File docfile = new File("books.xml");
      Document doc = null;
      try {
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       dbf.setNamespaceAware(true);
       DocumentBuilder db = dbf.newDocumentBuilder();
       doc = db.parse(docfile);
                                      Element root = doc.getDocumentElement();
       System.out.println("The root element is "+root.getNodeName());
                                     NodeList titles = root.getElementsByTagName("title");
       for(int titlenum=0;titlenum<titles.getLength();titlenum++){
        System.out.println("before change title :"+titles.item(titlenum).getFirstChild().getNodeValue());
       }
       DomBuilder.changebook(root, "title", "processed");
       NodeList titlesed = doc.getElementsByTagName("title");
       for(int titlenum=0;titlenum<titlesed.getLength();titlenum++){
        System.out.println("After change title :"+titlesed.item(titlenum).getFirstChild().getNodeValue());
       }
      } catch (ParserConfigurationException e) {
       System.out.println("The parser was not configured correctly.");
       System.exit(1);
      } catch (SAXException e) {
       System.out.println("Problem parsing the file.");
       System.exit(1);
      } catch (IOException e) {
       System.out.println("Cannot read input file.");
       System.exit(1);
      }

     }
     private static void changebook(Node start,String eleName,String eleValue){
      if(start.getNodeName().equals(eleName)){
       start.getFirstChild().setNodeValue(eleValue);
       System.out.println("set value finished !");
      }
      System.out.println("executed !");
      for(Node child=start.getFirstChild();child!=null;child=start.getNextSibling()){
       changebook(child,eleName,eleValue);
       System.out.println("递归调用 !");
      }
      
     }

    }

    执行之后,没有修改成功,有哪位高手能指点一下?谢谢!


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/5/10 15:40:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 DOM/SAX/XPath 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/20 0:28:21

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

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