尝试使用java解析带有转义字符的XML文件时出错

时间:2017-01-03 08:37:10

标签: java xml

尝试使用以下java代码解析其中包含转义字符的XML文件时出错。有没有办法在解析文件时处理它?

private Document parseXmlFile(String fileName) {
            Document doc = null;
            try {
                File fXmlFile = new File(fileName);
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                doc = dBuilder.parse(fXmlFile);
                doc.getDocumentElement().normalize();
            } catch (ParserConfigurationException | SAXException | IOException ex) {
                System.out.println(ex);
            }
            return doc;

        }

示例XML

<ResultDetail>
        <ObjectType>APF</ObjectType>
        <ObjectName>dlgCreateNewEmployee</ObjectName>
        <Header>AccessModifiers Detected</Header>
        <Description>&Original source: private</Description>
</ResultDetail>

1 个答案:

答案 0 :(得分:0)

   <ResultDetail>
    <ObjectType>APF</ObjectType>
    <ObjectName>dlgCreateNewEmployee</ObjectName>
    <Header>AccessModifiers Detected</Header>
    <Description>&#038;Original source: private</Description>

或者您也可以使用CDATA