/**
   * 以字符串形式读取xml文档
   * @param XMLString String 包含xml文档内容的字符串
   * @throws XmlOperationException 抛出自定义的xml操作异常
   * @return Document 返回读取结果的文档
   */
  public static Document readXmlDocument(String XMLString) throws
      XmlOperationException {
    return readXmlDocument(XMLString, new Boolean(true));
  }
  /**
   * 以字符串形式读取xml文档
   * @param XMLString String 包含xml文档内容的字符串
   * @param validate Boolean 是否校验标识
   * @throws XmlOperationException 抛出自定义的xml操作异常
   * @return Document 返回读取结果的文档
   */
  public static Document readXmlDocument(String XMLString, Boolean validate) throws
      XmlOperationException {    if ("".equals(checkEmpty(XMLString))) {
      LogUtil.log("[XML] 将XML字符串读入document时,XMLString为空或为“”。" +
                  "在XMLUtil方法readXmlDocument方法中。",
                  LogLevel.WARN);
      return null;
    }    ByteArrayInputStream bis = null;    try {      bis = new ByteArrayInputStream(XMLString.getBytes("UTF-8"));
    }
    catch (java.io.UnsupportedEncodingException e) {      e.printStackTrace();      LogUtil.log("[XML] 将XML字符串读入document时发生不支持字符集异常。" +
                  "在XMLUtil方法readXmlDocument方法中。异常信息:" +
                  e.getMessage(),
                  LogLevel.ERROR);
      throw new XmlOperationException(
          "将XML字符串读入document时发生不支持字符集异常。" +
          "在XMLUtil方法readXmlDocument方法中。", e);    }
    return readXmlDocument(bis, validate, "Internal Content");
  }

解决方案 »

  1.   

    不好意思整差了上面是dom4j的。jdom的没用过。
      

  2.   


    import java.io.*;
    import java.util.*;import org.jdom.*;
    import org.jdom.input.*;
    import org.jdom.output.*;
    import org.jdom.xpath.*;
    import com.zcjy.framework.common.log.*;
    import com.zcjy.framework.common.xml.*;public class TestJDomXMLStringRead {  private static Document readXMLDocument(String XMLString) {    Document document = null;
        ByteArrayInputStream bais = null;    try {
          bais = new ByteArrayInputStream(XMLString.getBytes("UTF-8"));
        }
        catch (UnsupportedEncodingException uee) {
          uee.printStackTrace();
        }    if (bais != null) {      try {
            SAXBuilder saxBuilder = new SAXBuilder();
            document = saxBuilder.build(bais);
          }
          catch (IOException ex) {
            ex.printStackTrace();
          }
          catch (JDOMException ex) {
            ex.printStackTrace();
          }
        }
        else {
          throw new NullPointerException("输入流为空!");
        }    return document;
      }  public static void main(String[] args) {    String testXMLString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><entitys><relations>" +
            "<relation relationName=\"class_student\" relationType=\"one-to-many\"><pkEntity name=\"class\">" +
            "<fields><field name=\"hh\"/><field name=\"id\"/></fields></pkEntity><fkEntity name=\"student\">" +
            "<fields><field name=\"id\"/><field name=\"bb\"/></fields></fkEntity></relation></relations></entitys>";
        
        Document document = readXMLDocument(testXMLString);    System.out.println(document.toString());
      }
    }
      

  3.   

    谢谢!问题已经解决了。InputStream和OutputStream是抽象类,不能直接使用。