you are trying to cast some variable to a non-compatible type

解决方案 »

  1.   

    你把一个不是org.apache.xerces.dom.DeferredDocumentImpl的造型为一个DeferredDocumentImpl了,仔细看看出错那行有没有造型操作。
      

  2.   

    程序如下:
    import java.io.*;
    import javax.xml.parsers.*;
    import org.xml.sax.*;
    import org.w3c.dom.*;
    import com.sun.xml.tree.*;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 1998</p>
     * <p>Company: </p>
     * @author mzer
     * @version 1.0
     */public class xmldo {        public xmldo() {
            }
            public static void main(String[] args) {
                    try
                    {
                            //turn xmlfile to byte[]
                            File f = new File("E:\\work\\tool\\src\\tool\\postoffice.xml");
                            FileInputStream fin = new FileInputStream(f);
                            byte[] byteIN = new byte[(int)f.length()];
                            fin.read(byteIN);
                            fin.close();
                            System.out.println("xmlFile open...");                        //append node                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                            DocumentBuilder db = dbf.newDocumentBuilder();
                            ByteArrayInputStream bf=new ByteArrayInputStream(byteIN);
                            Document m_dDoc=db.parse(bf);
                            //Document m_dDoc=db.getDOMImplementation();
                            Element eRoot=m_dDoc.getDocumentElement();
                            Element e1,e2;
                            e1=m_dDoc.createElement("id");
                            e2=m_dDoc.createElement("sql");
                            e1.appendChild(m_dDoc.createTextNode("111"));
                            eRoot.appendChild(e1);
                            ByteArrayOutputStream buf=new ByteArrayOutputStream();
                            //FileOutputStream fo=new FileOutputStream("E:\\work\\tool\\src\\tool\\postoffice.xml");
                            //System.out.println("xmlFile open...4");
                            ((XmlDocument)m_dDoc).write(buf);
                            byte[] bArray=buf.toByteArray();
                            FileOutputStream fo=new FileOutputStream("E:\\work\\tool\\src\\tool\\postoffice.xml");
                            fo.write(bArray);
                            fo.close();
                    }
                    catch(Exception e)
                    {
                            e.printStackTrace();
                    }        }
    }