我现在用dom解析xml,想设置一个节点的属性,
比如<book>测试</book>设置成<book id=1>测试book>
或者比如<book id=0>测试</book>设置成<book id=1>测试book>请问怎么解决呢另外:我现在想把这个文件整体的内容取出来,写成string的,怎么解决呢,
大哥大姐们,帮帮我,最好把关键地方的代码写给我看看
谢谢!

解决方案 »

  1.   

    dom4j我记得是个开源项目中的,可以参考一下。
      

  2.   

    楼主的意思是把<book id=0>测试</book>设置成<book id=1>测试book>再写回到XML文件中吗?
      

  3.   

    我就是这个意思,我也是dom的,可是我实现不了,请诸位指教啊,最好把相关的代码写给我看,谢谢啊我是这样的:
     static String parserXML(Document xmlDB,Document docFile) throws Exception{
            
            //处理
         Element root = xmlDB.getDocumentElement();
         Element rootFile = docFile.getDocumentElement();
         //System.out.println(root.getNodeName()+"--"+rootFile.getNodeName());        NodeList children = root.getChildNodes();
            NodeList childrenFile = rootFile.getChildNodes();
            
            for(int i=0;i<children.getLength();i++){
             Node childrenN=children.item(i);
             for(int j=0;j<childrenFile.getLength();j++){
             Node childrenFileN=childrenFile.item(j);
            
             if((childrenN.getNodeType()==childrenN.ELEMENT_NODE)&&(childrenFileN.getNodeType()==childrenFileN.ELEMENT_NODE)){
             if(children.item(i).getNodeName()==childrenFile.item(j).getNodeName()){
             NamedNodeMap startAttr = childrenN.getAttributes();
             //childrenN.g
             for (int k = 0; k < startAttr.getLength();k++) 
             {
             Node attr = startAttr.item(k);
             //Node attr = startAttr.item(k);
             //attr.
             //childrenFile.item(j).
             //childrenFile.
             //childrenFileN.;
         
             System.out.println("  Attribute:  "+ attr.getNodeName() +" = "+attr.getNodeValue());
             } 
             }
             }
                }
            }        //System.out.println("The root element is "+root.getNodeName());
            return null;
        }我现在用另外的一个xml 来更新他,可是我不会,请知道我啊