读取转义报价作为xml的转义报价

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

  ff1 " ff1 "  

结果:

 true ff1 " ff1 " 

期望的结果:

 false ff1 " ff1 " 

所以我需要区分这两种情况:当报价被转义而不是。

请帮忙。

先谢谢你。

PS 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--; } } } 

在开始解析之前,您只需要在main方法中添加下一行

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

你会得到预期的结果:

 false ff1 " ff1 " 

看起来你可以得到TEXT_NODE子节点并使用getNodeValue (假设它不是NULL):

 public static String getRawContent(Node n) { if (n == null) { return null; } Node n1 = getChild(n, Node.TEXT_NODE); if (n1 == null) { return null; } return n1.getNodeValue(); } 

从以下url获取: http : //www.java2s.com/Code/Java/XML/Gettherawtextcontentofanodeornullifthereisnotext.htm

没有办法为内部实体执行此操作。 XML不支持这个概念。 内部实体只是将相同的PSVI内容写入文本的不同方式,它们并不是独特的。