示例#1
0
  public static org.dom4j.Element readStrToDOM4J(final String data) throws Exception {
    SAXReader saxReader = new SAXReader();

    saxReader.setValidation(false);
    saxReader.setStripWhitespaceText(true);
    saxReader.setFeature("http://xml.org/sax/features/namespaces", false);
    saxReader.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", false);

    // saxReader.setFeature("http://apache.org/xml/features/validation/schema", false);
    // saxReader.setFeature("http://xml.org/sax/features/validation", false);
    // saxReader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
    //                   (FormViewFactory.class.getResource("../form.xsd")).getPath());

    org.dom4j.Document document = saxReader.read(new StringReader(data));
    return document.getRootElement();
  }
  @Override
  public P parse(final InputStream stream) throws IOException {
    final SAXReader reader = new SAXReader();

    // Remove nodes generated by indentation.
    reader.setStripWhitespaceText(true);
    reader.setMergeAdjacentText(true);

    final Document domdoc;
    try {
      domdoc = reader.read(stream);
    } catch (DocumentException e) {
      throw new IOException("Failed to parse XML, probably malformed input.");
    }
    return this.parse(domdoc.getRootElement());
  }