【问题标题】:Not able to write xml with iso encoding to Marklogic via JAVA API无法通过 JAVA API 将带有 iso 编码的 xml 写入 Marklogic
【发布时间】:2015-05-04 10:26:42
【问题描述】:

我们正在尝试通过 JAVA API 将 ISO 编码的 xml 插入 MarkLogic,但出现此错误。 xml中包含特殊字符,例如:注册商标标志-

®

Bad Request. Server Message: XDMP-DOCUTF8SEQ: Invalid UTF-8 escape sequence at  line 14145 -- document is not UTF-8 encoded. 

代码:

DatabaseClient client = DatabaseClientFactory.newClient(IP, PORT,
                DATABASE_NAME, USERNAME, PWD, Authentication.DIGEST);
            // acquire the content
            InputStream xmlDocStream = XMLController.class.getClassLoader()
                    .getResourceAsStream("path to xml file");

            // create a manager for XML documents
            XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager();

            // create a handle on the content
            InputStreamHandle xmlhandle = new InputStreamHandle(xmlDocStream);

            // write the document content
            xmlDocMgr.write("/" + filename, xmlhandle);

【问题讨论】:

  • 如果我们使用 mlcp (marklogic content pump) 加载这个 xml,我们会得到同样的错误
  • 是的,代码是……?
  • 您基本上已经问过这个问题,但措辞略有不同? stackoverflow.com/questions/30028454/…
  • @fge 我们已经添加了相关代码。

标签: java xml encoding utf-8 marklogic


【解决方案1】:

斯拉万:

解决方案是通过将输入流包装在 InputStreamReader 中来指定读取资源时的当前 ISO 编码:

http://docs.oracle.com/javase/8/docs/api/java/io/InputStreamReader.html#InputStreamReader-java.io.InputStream-java.lang.String-

当 Java API 知道内容具有不同的编码但假定内容已经是 UTF-8 时,它会转换为 UTF-8。更多关于编码转换的详细信息,请参见:

http://docs.marklogic.com/guide/java/document-operations#id_11208

希望对您有所帮助,

【讨论】:

  • 我尝试使用 XMLEventReaderHandle 和 XMLStreamReaderHandle。这对于使用 UTF-8 编码的 xml 文件来说效果很好,即插入和检索都成功(这里 Marklogic 将特殊字符转换为其他形式,并且在检索时显示正确的形式)但是当我尝试使用 xml 时ISO 编码,我可以插入但无法检索文件中的特殊字符。
  • MarkLogic 始终将 JSON、文本和 XML 文档存储为 UTF-8。如果应用程序或工具需要读取其他编码(例如 ISO-8859-1)的文档,您可以使用 InputStreamHandle 在文档上打开 InputStream,然后使用指定字符集的 Reader(或其他读取 API)包装它为所需的编码。
猜你喜欢
  • 2012-01-17
  • 1970-01-01
  • 2023-04-07
  • 2010-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-20
  • 2020-05-22
相关资源
最近更新 更多