DocumentBuilder在解析xml时删除\ n和\ t

时间:2012-09-04 10:04:49

标签: java saxparser

  

可能重复:
  How to keep whitespace before document element when parsing with Java?

我正在尝试解析xml,以便我可以替换拉丁字符。 xml中的拉丁字符将被替换,但xml中的\ n和\ t字符将被删除。我正在使用dtd替换拉丁字符。 下面是我用于解析xml的代码

DocumentBuilderFactory docFactory = DocumentBuilderFactory
                .newInstance();
        docFactory.setValidating(false);
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        docBuilder.setEntityResolver(resolver);
        String str = "";
        try {
            Document doc = docBuilder.parse(is, contextPath);
            OutputFormat format = new OutputFormat(doc); // Serialize DOM
            format.setIndenting(reqIndent);
            format.setOmitDocumentType(true);
            format.setEncoding(encoding);
            StringWriter stringOut = new StringWriter(); // Writer will be a
            XMLSerializer serial = new XMLSerializer(stringOut, format);
            serial.asDOMSerializer(); // As a DOM Serializer
            serial.serialize(doc);
            str = stringOut.toString();
        } catch (org.xml.sax.SAXParseException saxExp) {
            throw saxExp; // asString(is);
        } catch (Exception e) {

            throw e;

        } finally {
            if (is != null)
                is.close();
        }
        return str;

0 个答案:

没有答案