java - 将转义引号读取为 xml 中的转义引号

标签 java xml escaping

我将 xml 文件加载到 DOM 模型中并对其进行分析。

其代码是:

public class MyTest {
public static void main(String[] args) {        
    Document doc = XMLUtils.fileToDom("MyTest.xml");//Loads xml data to DOM
    Element rootElement = doc.getDocumentElement();
    NodeList nodes = rootElement.getChildNodes();
    Node child1 = nodes.item(1);
    Node child2 = nodes.item(3);
    String str1 = child1.getTextContent();
    String str2 = child2.getTextContent();      
    if(str1 != null){
        System.out.println(str1.equals(str2));
    }
    System.out.println();
    System.out.println(str1);
    System.out.println(str2);
}   

}

MyTest.xml

<tests>
   <test name="1">ff1 &quot;</test>
   <test name="2">ff1 "</test>
</tests>

结果:

true

ff1 "
ff1 "

期望的结果:

false

ff1 &quot;
ff1 "

所以我需要区分这两种情况:当引号被转义时和未被转义时。

请帮忙。

提前谢谢您。

附注XMLUtils#fileToDom(String filePath) 的代码,来自 XMLUtils 类的片段:

static {
    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
    dFactory.setNamespaceAware(false);
    dFactory.setValidating(false);
    try {
        docNonValidatingBuilder = dFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
    }
}

public static DocumentBuilder getNonValidatingBuilder() {
    return docNonValidatingBuilder;
}

public static Document fileToDom(String filePath) {

    Document doc = getNonValidatingBuilder().newDocument();
    File f = new File(filePath);
    if(!f.exists())
        return doc;

    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        DOMResult result = new DOMResult(doc);
        StreamSource source = new StreamSource(f);
        transformer.transform(source, result);
    } catch (Exception e) {
        return doc;
    }

    return doc;

}

最佳答案

我查看了 apache xerces 的源代码并提出了我的解决方案(但它是猴子补丁)。 我写了一个简单的类

package a;
import java.io.IOException;
import org.apache.xerces.impl.XMLDocumentScannerImpl;
import org.apache.xerces.parsers.NonValidatingConfiguration;
import org.apache.xerces.xni.XMLString;
import org.apache.xerces.xni.XNIException;
import org.apache.xerces.xni.parser.XMLComponent;

public class MyConfig extends NonValidatingConfiguration {

    private MyScanner myScanner;

    @Override
    @SuppressWarnings("unchecked")
    protected void configurePipeline() {
        if (myScanner == null) {
            myScanner = new MyScanner();
            addComponent((XMLComponent) myScanner);
        }
        super.fProperties.put(DOCUMENT_SCANNER, myScanner);
        super.fScanner = myScanner;
        super.fScanner.setDocumentHandler(this.fDocumentHandler);
        super.fLastComponent = fScanner;
    }

    private static class MyScanner extends XMLDocumentScannerImpl {

        @Override
        protected void scanEntityReference() throws IOException, XNIException {
            // name
            String name = super.fEntityScanner.scanName();
            if (name == null) {
                reportFatalError("NameRequiredInReference", null);
                return;
            }

            super.fDocumentHandler.characters(new XMLString(("&" + name + ";")
                .toCharArray(), 0, name.length() + 2), null);

            // end
            if (!super.fEntityScanner.skipChar(';')) {
                reportFatalError("SemicolonRequiredInReference",
                        new Object[] { name });
            }
            fMarkupDepth--;
        }
    }

}

在开始解析之前,您只需将下一行添加到主方法

System.setProperty(
            "org.apache.xerces.xni.parser.XMLParserConfiguration",
            "a.MyConfig");

你将会得到预期的结果:

false

ff1 &quot;
ff1 "

关于java - 将转义引号读取为 xml 中的转义引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1979785/

相关文章:

bash - 为什么一定要逃走 |和 + 在撇号之间的 grep?

java - struts 1 验证不起作用

java - 我怎样才能生成这个错误

java - JSF Primefaces 和 Spring security 用户管理 ROLE_USER 和 ROLE_ADMIN

php - 将 foreach 拆分为页面

java - 从 JSON 创建 XML

java - 在 Java 中用特殊字符前面的转义符替换特殊字符

c# - 如何在 C# 中解码 URL 转义字符串?

java - 以 html 格式显示输出

javascript - 如何使用ajax和javascript保存xml文件