【问题标题】:How to get String content of Document object(including markup)?如何获取 Document 对象的字符串内容(包括标记)?
【发布时间】:2011-07-31 11:48:06
【问题描述】:

DocumentBuilderFactory 工厂 = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

文档 doc = builder.newDocument();

//这里追加一些子项.... (这个应用程序应该在 andoroid API 4 上运行) 问题是:如何获取“doc”的文本内容(包括标记)? 如果有人能给我一些建议,我将不胜感激。谢谢~!

【问题讨论】:

    标签: android xml text document


    【解决方案1】:
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(d), new StreamResult(sw));
        System.out.println(sw.toString());
    

    【讨论】:

    • 感谢您的回答。但是,我的应用程序在 API 4 平台上运行,而“TransformerFactory”类需要 API 8 或更高版本。那么,您还有其他方法可以解决这个问题吗?
    • 嗯? API 4,API 8,我们在谈论 Android 吗?也许你应该这样标记它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2018-04-01
    • 2011-03-10
    • 2021-07-03
    • 1970-01-01
    相关资源
    最近更新 更多