【问题标题】:Java XPath umlaut/vowel parsingJava XPath 变音/元音解析
【发布时间】:2015-07-16 02:38:06
【问题描述】:

我要解析以下xml结构:

<?xml version="1.0" encoding="utf-8"?>
<documents>
  <document>
    <element name="title">
      <value><![CDATA[Personnel changes: Müller]]></value>
    </element>
  </document>
</documents>

为了解析这个element name="????? 结构,我按以下方式使用XPath:

XPath xPath = XPathFactory.newInstance().newXPath();

String currentString = (String) xPath.evaluate("/documents/document/element[@name='title']/value",pCurrentXMLAsDOM, XPathConstants.STRING);

解析本身可以正常工作,但德语变音符号(元音)如“Ü”、“ß”或类似的东西存在一些问题。当我打印出 currentString 时,字符串是:

Personnel changes: Müller

但我想拥有像 XML 中的字符串:

Personnel changes: Müller

补充一句:我不能改变xml文件的内容,我必须像我得到它一样解析它,所以我必须以正确的方式解析everey String。

【问题讨论】:

  • 您的 JVM 使用什么编码运行?您可以使用 -Dfile.encoding 将其设置为 UTF-8 吗?
  • 感谢您的评论。如何以编程方式更改它?我认为 -Dfile.encoding 是命令行参数不是吗?但是如果我必须改变它,我想在我的代码中做它
  • System.out.println(System.getProperty("file.encoding")); return "Cp1252"
  • pCurrentXMLAsDOM 来自哪里?它是从文件中读取的吗?怎么样?
  • 首先我在 BufferedReader (FileReader) 中读取 xml 并将其存储到字符串中。然后我去除无效字符(因为 xml 包含我无法以正常方式解析的二进制数据),然后我将字符串转换为文档对象,这一切都很好,我可以准确地读取每个 xml 元素。字符集只有这些问题

标签: java parsing xpath


【解决方案1】:

听起来像是编码问题。 XML 是 UTF-8 编码的 Unicode,您似乎将其打印为 ISO-8859-1。检查 Java 源代码的编码设置。

编辑:请参阅Setting the default Java character encoding?了解如何设置file.encoding

【讨论】:

  • 很抱歉,我在此链接中没有看到好的解决方案。好的,他们告诉您可以将操作系统环境变量JAVA_TOOL_OPTIONS 更改为-Dfile.encoding=UTF8 bt 这对我来说不是解决方案。我无法更改此变量,因为我的应用程序必须在每台电脑上运行而不更改系统变量。
  • 也只是为了好玩,我只是尝试将 -Dfile.encoding="UTF-8" 设置为 JVM 参数,它还将默认字符集更改为 UTF-8,但字符串仍然错误: Personnel changes: Müller
【解决方案2】:

我现在找到了一个又好又快的解决方案:

public static String convertXMLToString(File pCurrentXML) {

        InputStream is = null;
        try {
            is = new FileInputStream(pCurrentXML);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        String contents = null;
         try {

                try {
                    contents = IOUtils.toString(is, "UTF-8");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } finally {
                IOUtils.closeQuietly(is);
            }

        return contents;

    }

之后我将 String 转换为 DOM 对象:

static Document convertStringToXMLDocumentObject(String string) {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        Document document = null;

        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }

        try {
            document = builder.parse(new InputSource(new StringReader(string)));
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return document;

    }

然后我可以使用 XPath 解析 DOM,所有元素值都在 UTF-8 中!! 示范:

currentString = (String) xPath.evaluate("/documents/document/element[@name='title']/value",pCurrentXMLAsDOM, XPathConstants.STRING);
System.out.println(currentString);

输出:

Personnel changes: Müller

:)

【讨论】:

    【解决方案3】:

    如果您知道文件是 utf8 编码的,请尝试类似:

        FileInputStream fis = new FileInputStream("yourfile.xml");
        InputStreamReader in = new InputStreamReader(fis, "UTF-8");
    
        InputSource pCurrentXMLAsDOM = new InputSource(in);
    

    【讨论】:

    • 这是个好主意,但 InputSource 来自 SAXParser 对吗?而且我不知道如何用 SAX 解析我的 xml 文件,因为:&lt;element name="title"&gt; &lt;value&gt;&lt;![CDATA[Personnel changes: Müller]]&gt;&lt;/value&gt; &lt;/element&gt;
    猜你喜欢
    • 1970-01-01
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 2023-03-05
    • 2014-05-10
    • 1970-01-01
    相关资源
    最近更新 更多