Attr att_id = doc.createAttribute( ATT_ID + nAttID );
Node node.appendChild( att_id ); //此句为何出错

解决方案 »

  1.   

    Node node = new Node();
    node.appendChild( att_id );
      

  2.   

    先确认你的错误不在这三种情况之中
    1.DOMException - HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to append is one of this node's ancestors or this node itself.
    2.WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node. 
    3.NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or if the previous parent of the node being inserted is readonly.
      

  3.   

    node.appendChild( att_id ); 不能使用
    setAttributeNode 方法是哪个类的
      

  4.   

    说明Node node.appendChild( att_id ); //此句为何出错
    这句话前面的Node 为说明用
      

  5.   

    /**
         *
         * @param node
         * @param nAttID
         * @param xmlFile
         */
        protected static void addAttribute( String strXmlFile,String strNode, int nAttID )
        {
             Document doc = WriteXml.getDocument( strXmlFile );         //get node in which new columns are inserted into
             Element node = (Element)WriteXml.getChildNode( doc,strNode );         Attr att_id = doc.createAttribute( ATT_ID + nAttID );
             Attr att_len = doc.createAttribute( ATT_LEN + nAttID );
             Attr att_dt = doc.createAttribute( ATT_DT + nAttID );
             Attr att_header = doc.createAttribute( ATT_HEADER + nAttID );         att_id.setNodeValue( "value_instr_" + nAttID );
             att_len.setNodeValue( "8" );
             att_dt.setNodeValue( "Char" );
             att_header.setNodeValue( "" );         node.setAttributeNode( att_id ) ;
             node.setAttributeNode( att_len );
             node.setAttributeNode( att_dt );
             node.setAttributeNode( att_header );         try
             {
                 TransformerFactory tFactory = TransformerFactory.newInstance();
                 Transformer transformer = tFactory.newTransformer();             /* method one*/
                 //java.util.Properties  prop = transformer.getOutputProperties();
                 //prop.setProperty( OutputKeys.ENCODING, "GB2312" );
                 //prop.setProperty( OutputKeys.METHOD, "xml" );
                 //prop.setProperty( OutputKeys.INDENT, "yes" );
                 //transformer.setOutputProperties( prop );             /*method 2*/
                 //OutputFormat formater = new OutputFormat( doc,"GB2312",true );
                 DOMSource source = new DOMSource( doc );
                 File xmlFile = new File( strXmlFile );
                 StreamResult result = new StreamResult( xmlFile );             transformer.transform( source, result );
             }
             catch( Exception ex )
             {
                ex.printStackTrace();
             }
        }