public static Document string2Document(String str) {
    if(str==null||str.equals("")) return null;    try {
      org.xml.sax.InputSource is=new org.xml.sax.InputSource();
      is.setEncoding("GB2312");
      java.io.StringReader sr=new java.io.StringReader(str);
      is.setCharacterStream(sr);
      Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
      return doc;
    }
    catch (Exception ex) {
      logger.error(ex);
      return null;
    }
  }
//use ' instead of "
  String s="
<?xml version="1.0" encoding="gb2312" ?> <Message Version="1.0" xmlns="http://www.egxml.gov.cn/message"
smlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.egxml.gov.cn/message message.xsd">
<egXML xmlns="http://www.egxml.gov.cn/egXML" Version="1.9">
</egXML>
<display>
</display>
<eSign signFlag="false" verifyFlag="false" />
<eSealContainer />
</Message>
";Document dom=string2Document(s);
dom.....

解决方案 »

  1.   

    package com.tjsoft.test;import java.io.*;
    import org.jdom.*;
    import org.jdom.input.*;
    import org.jdom.output.*;public class CXmlMaker {
      public CXmlMaker() {
      }
      public void BuildXMLDoc() throws IOException,JDOMException {
        Element eRoot,e1,e2;
        Document doc;
        eRoot = new Element("Message");
        doc = new Document(eRoot);
        eRoot = doc.getRootElement();
        eRoot.addAttribute("Version","1.0");
    //    eRoot.addAttribute("xmlns","http://www.egxml.gov.cn/message");
    //    eRoot.addAttribute("smlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
    //    eRoot.addAttribute("xsi:schemaLocation","http://www.egxml.gov.cn/message message.xsd");
        e1 = new Element("egXML");
        e2 = e1.setText("aaa");
        e1 = eRoot.addContent(e2);
        e1.addAttribute("Version","1.9");
        e1 = new Element("display");
        e2 = e1.setText("bbb");
        e1 = eRoot.addContent(e2);
        e1 = new Element("eSign");
        e2 = e1.setText("ccc");
        e1 = eRoot.addContent(e2);
        e1 = new Element("eSealContainer");
        e2 = e1.setText("ddd");
        e1 = eRoot.addContent(e2);
        XMLOutputter XMLOut = new XMLOutputter();
        XMLOut.setEncoding("gb2312");
        XMLOut.output(doc, new FileOutputStream("test1.xml"));
      }
      public static void main(String[] args) {
            try {
               CXmlMaker s1 = new CXmlMaker();
                System.out.println("Now we build an XML document .....");
                s1.BuildXMLDoc();
            }
    catch (Exception e) {
               System.out.println(e.getMessage());
            }
        System.out.println("Now we build an XML document .....");
        }
    }我的程序只能生成
     <?xml version="1.0" encoding="gb2312" ?> 
     <Message Version="1.0">
      <egXML>aaa</egXML> 
      <display>bbb</display> 
      <eSign>ccc</eSign> 
      <eSealContainer>ddd</eSealContainer> 
      </Message>
    但是smlns:xsi不是attribute,应该怎么生成呢?
      

  2.   

    to:vcvj(福州,买房还款,挣$,想去厦门,想车ing)
    你那个程序需要哪些包?
    我用你的程序,生成一个
    Document dom=string2Document(s);
       XMLOutputter XMLOut = new XMLOutputter();
       XMLOut.setEncoding("gb2312");
       XMLOut.output(dom, new FileOutputStream("test1.xml"));
    然后怎么导出啊?
    XMLOut.output(dom, new FileOutputStream("test1.xml"));这句报错了
      

  3.   

    import org.apache.crimson.tree.XmlDocument;
        public static void outDocument(java.io.PrintStream out,Document doc){
          if(doc==null||out==null){
            logger.error("The parameter DOM and stream are need.");
          }
          try{
            ( (XmlDocument) doc).write(out);
          }catch(java.io.IOException ioe){
            logger.error(ioe);
          }
        }
      

  4.   

    多谢vcvj(福州,买房还款,挣$,想去厦门,想车ing)
    我是初学者,对这些不太熟悉,麻烦你再指点一下,
    那我怎样把它输出到一个xml文件里?
    outDocument(new java.io.PrintStream("test1.xml"),doc);
    应该不是这样吧,这样会报错,
    应该怎么写啊,这最后一句?
    多谢了,非常感激
    我本来用jdom的,但是不知道那个smlns:xsi怎么才能生成,
    现在你这个好象也比较方便,就差这最后一点点啦
      

  5.   

    Now we build an XML document .....
    org.xml.sax.SAXParseException: Element type "Message" must be followed by either attribute specifications, ">" or "/>".
    at org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1196)
    at org.apache.xerces.framework.XMLDocumentScanner.reportFatalXMLError(XMLDocumentScanner.java:579)
    at org.apache.xerces.framework.XMLDocumentScanner.abortMarkup(XMLDocumentScanner.java:628)
    at org.apache.xerces.framework.XMLDocumentScanner.scanElement(XMLDocumentScanner.java:1800)
    at org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:949)
    at org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381)
    at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1081)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:195)
    at com.tjsoft.test.CXmlMaker.string2Document(CXmlMaker.java:51)
    at com.tjsoft.test.CXmlMaker.main(CXmlMaker.java:94)
    doc is null or out is null
    null
    Now we build an XML document .....大哥,报错啊,我的程序是这样的
    帮忙看一下
    package com.tjsoft.test;import java.io.*;
    import org.jdom.*;
    import org.jdom.input.*;
    import org.jdom.output.*;
    import org.xml.sax.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.Document;
    import org.apache.crimson.tree.XmlDocument;public class CXmlMaker {
      public CXmlMaker() {
      }
    //  public void BuildXMLDoc() throws IOException,JDOMException {
    //    Element eRoot,e1,e2;
    //    Document doc;
    //    eRoot = new Element("Message");
    //    doc = new Document(eRoot);
    //    eRoot = doc.getRootElement();
    //    eRoot.addAttribute("Version","1.0");
    //    eRoot.addAttribute("xmlns","http://www.egxml.gov.cn/message");
    //    eRoot.addAttribute("smlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
    //    eRoot.addAttribute("xsi:schemaLocation","http://www.egxml.gov.cn/message message.xsd");
    //    e1 = new Element("egXML");
    //    e2 = e1.setText("aaa");
    //    e1 = eRoot.addContent(e2);
    //    e1.addAttribute("Version","1.9");
    //    e1 = new Element("display");
    //    e2 = e1.setText("bbb");
    //    e1 = eRoot.addContent(e2);
    //    e1 = new Element("eSign");
    //    e2 = e1.setText("ccc");
    //    e1 = eRoot.addContent(e2);
    //    e1 = new Element("eSealContainer");
    //    e2 = e1.setText("ddd");
    //    e1 = eRoot.addContent(e2);
    //    XMLOutputter XMLOut = new XMLOutputter();
    //    XMLOut.setEncoding("gb2312");
    //    XMLOut.output(doc, new FileOutputStream("test1.xml"));
    //  }
      
      public static Document string2Document(String str) {
        if(str==null||str.equals("")) return null;    try {
          org.xml.sax.InputSource is=new org.xml.sax.InputSource();
          is.setEncoding("GB2312");
          java.io.StringReader sr=new java.io.StringReader(str);
          is.setCharacterStream(sr);
          Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
          return doc;
        }
        catch (Exception ex) {
          ex.printStackTrace();
          //logger.error(ex);
          return null;
        }
      }
      
      public static void outDocument(java.io.PrintStream out,Document doc){
          if(doc==null||out==null){
            System.out.println("doc is null or out is null");
            //logger.error("The parameter DOM and stream are need.");
          }
          try{
            ( (XmlDocument) doc).write(out);
          }catch(java.io.IOException ioe){
            //logger.error(ioe);
            ioe.printStackTrace();
          }
        }
    //use ' instead of "
        public static void main(String[] args) {
            try {
               CXmlMaker s1 = new CXmlMaker();
                System.out.println("Now we build an XML document .....");
               // s1.BuildXMLDoc();
                String s="<?xml version=\"1.0\" encoding=\"gb2312\" ?> <Message Version=\"1.0\" "
           + "xmlns=\"http://www.egxml.gov.cn/message\" smlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
           + "xsi:schemaLocation=\"http://www.egxml.gov.cn/message message.xsd\">"
           + "<egXML xmlns=\"http://www.egxml.gov.cn/egXML\" Version=\"1.9\">"
           + "</egXML>"
           + "<display>"
           + "</display>"
           + "<eSign signFlag=\"false\" verifyFlag=\"false\" />"
           + "<eSealContainer />"
           + "</Message>";   Document doc=string2Document(s);
    //   XMLOutputter XMLOut = new XMLOutputter();
    //   XMLOut.setEncoding("gb2312");
    //   XMLOut.output(doc, new FileOutputStream("test1.xml"));
    //   String ss = doc.toString();
    //   System.out.println(ss);
       java.io.PrintStream out = null;
       outDocument(out,doc);
       String aa = out.toString();
       System.out.println(aa);
               }
    catch (Exception e) {
               System.out.println(e.getMessage());
            }
        System.out.println("Now we build an XML document .....");
        }
    }
      

  6.   

    首先 你的去下载jdom1.0最新的包,下面是
    public void BuildXMLDoc() throws IOException, JDOMException
      {
        Element eRoot, e1, e2;
        Document doc;
        eRoot = new Element("Message");
        doc = new Document(eRoot);
        eRoot = doc.getRootElement();
        eRoot.setAttribute("Version", "1.0");
        eRoot.setAttribute("Xmlns", "http://www.egxml.gov.cn/message");
        eRoot.setAttribute("Smlnsxsi", "http://www.w3.org/2001/XMLSchema-instance");
        eRoot.setAttribute("XsischemaLocation", "http://www.egxml.gov.cn/message message.xsd");
        e1 = new Element("egXML");
        e2 = e1.setText("aaa");
        e1 = eRoot.addContent(e2);
        e1.setAttribute("Version", "1.9");
        e1 = new Element("display");
        e2 = e1.setText("bbb");
        e1 = eRoot.addContent(e2);
        e1 = new Element("eSign");
        e2 = e1.setText("ccc");
        e1 = eRoot.addContent(e2);
        e1 = new Element("eSealContainer");
        e2 = e1.setText("ddd");
        e1 = eRoot.addContent(e2);
        
        //Format format = Format.getCompactFormat();
        Format format = Format.getPrettyFormat();
        //Format format = Format.getRawFormat();
        format.setEncoding("gb2312");
        XMLOutputter XMLOut = new XMLOutputter(format);
        
        //XMLOut.setEncoding("gb2312");
        XMLOut.output(doc, new FileOutputStream("test1.xml"));
      }就可以解决你的问题,
    但是发现问题:
    eRoot.setAttribute("Smlnsxsi", "http://www.w3.org/2001/XMLSchema-instance");
        eRoot.setAttribute("XsischemaLocation", "http://www.egxml.gov.cn/message message.xsd");
    加上“:”是出错的 
    eRoot.setAttribute("Smlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        eRoot.setAttribute("Xsi:schemaLocation", "http://www.egxml.gov.cn/message message.xsd");用dom4j是没有问题的