使用 StaX 如何读取带有 & 字符的 UTF-8 数据?

Using StaX how to read UTF-8 data with & characters?

如何使用 Stax 读取标签文本中的所有字符,甚至 &? 我对传入的 XML 文件没有影响。

示例 XML 文件是:

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
    <Employee id="1">
        <age>22</age>
        <name>MyName &amp; Team 01/46</name>
        <gender>Female</gender>
        <role>Java Developer</role>
    </Employee>
    ....
</Employees>

经过多次尝试,从名称中只读取了“MyName”部分。

尝试 1:

Path gpxPath = Paths.get( path);
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLStreamReader reader;
reader = xmlInputFactory.createXMLStreamReader( new FileInputStream(gpxPath.toFile()), "UTF-8");
... 
String name = reader.getText();

尝试 2:

XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
try {
    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader( 
          new DataInputStream(new FileInputStream(fileName)), "UTF-8");
    ... 
    name = new String( xmlStreamReader.getTextCharacters());
    // or ... 
    name = xmlStreamReader.getText();

如何读完整的名字?所以,“MyName & Team 01/46”。

解决方案是在 Xml 工厂上设置一个 属性:

XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty( IS_COALESCING, true);