以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  [原创]Dom解析xml文件中,如何更改节点的值?代码查错,请教高手!  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=46684)


--  作者:simoon1981
--  发布时间:5/10/2007 3:40:00 PM

--  [原创]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("递归调用 !");
  }
  
 }

}

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


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.500ms