gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:57:13 24 4
gpt4 key购买 nike

我将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com