【问题标题】:Inserting a CDATA section into XML generated by a Restlet web service using SAX使用 SAX 将 CDATA 部分插入到由 Restlet Web 服务生成的 XML 中
【发布时间】:2014-12-18 19:22:18
【问题描述】:

我需要在我的一些 XML 节点中嵌入一些 HTML,而消费者希望它在 CDATA 部分中。他们希望将 CDATA 保留为<![CDATA[ ]]>.,但开始和结束尖括号被转义,因此他们收到的是<![CDATA[]]>。我尝试了许多不同的方法,但似乎没有任何效果。 Restlet 框架中似乎没有内置任何功能。有没有其他人遇到过这个问题并找到了解决方案?这是我的代码示例:

@Get
public Representation toXml() {

    // Create a new SAX representation
    SaxRepresentation result = new SaxRepresentation() {

        public void write(org.restlet.ext.xml.XmlWriter writer)
                throws IOException {
...

        try {
                // Start document
                writer.startDocument();

                // Append the root node
                writer.startElement(COMPANIES); 

      // I want to start the CDATA section here.

      writer.startElement(BRANCHES);
                if((mfgRepList != null) && (mfgRepList.size() > 0)){
                    writer = MfgRep.getMfgRepHtml(writer, mfgRepList);  
                }
                writer.endElement(BRANCHES);

      // I want to end the CDATA section here.


      writer.endElement(COMPANIES);

      writer.endDocument();
    } catch (SAXException e) {
                throw new IOException(e.getMessage());
            }
        }; // end of write

    }; // end of create new Sax representation

    return result;
}

【问题讨论】:

    标签: xml restlet cdata


    【解决方案1】:

    检查 restlet 代码后,看起来它们“转义”了所有字符串/字符。所以,在这种情况下,我相信唯一的解决方案是通过输出流围绕 xml 编写器“写入”:

        writer.getWriter().write("<![CDATA[");
        // insert CDATA data here
        writer.getWriter().write("]]");
    

    可能还有其他解决方案,但在这种情况下,这似乎是最直接的。我相信 这是restlet api中的一个疏忽。

    【讨论】:

    • 好样的,格雷迪!那成功了。我真的很感谢你的努力。 Stackoverflow 再次出现。这必须是地球上最伟大的智囊团之一。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 2015-10-17
    • 2021-11-15
    相关资源
    最近更新 更多