- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Java 代码
String xml = "/Users/test/xml/test.xml";
// long startTime = System.nanoTime();
Source xmlFile = new StreamSource(new File(xml));
System.out.println(xmlFile.getSystemId() + " is valid");
XML 文件是
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE foo [ <!ELEMENT foo ANY >
<!ENTITY test SYSTEM "file:///Users/test/list" >]>
<cds>
<user>&test;</user>
当我按照我的理解执行代码时, getSystemId 应该打印“file:///Users/test/list”,但它打印“/Users/test/xml/test.xml”。我的代码是否有错误,或者有什么方法可以提取systemID?
最佳答案
你的推理与你的代码正在做的事情有点偏离。代码正在做它应该做的事情。
根据文档StreamSource#getSystemId()
方法返回:
The system identifier that was set with setSystemId, or null if setSystemId was not called.
您得到的输出是正确的,因为当您提供 new File(xml)
时至StreamSource
构造函数,根据StreamSource
它接受的类定义如下:
/**
* Construct a StreamSource from a File.
*
* @param f Must a non-null File reference.
*/
public StreamSource(File f) {
//convert file to appropriate URI, f.toURI().toASCIIString()
//converts the URI to string as per rule specified in
//RFC 2396,
setSystemId(f.toURI().toASCIIString());
}
所以,这就是为什么你会得到实际的文件名 /Users/test/xml/test.xml
你提供的,而不是你推理的意图。
我猜你可能期望它会奇迹般地从你的 xml
中提取它。文件,在 <!ENTITY test SYSTEM "file:///Users/test/list" >]>
行中。这是错误的!
尝试谷歌搜索一些 xml 解析库,或者自己解析文件内容。
仅供引用:- 您始终可以查看您在 IDE 中使用的类的代码。
引用:https://docs.oracle.com/javase/7/docs/api/javax/xml/transform/stream/StreamSource.html#getSystemId()
关于java - getSystemId()的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60083795/
我有一个 Java 代码 String xml = "/Users/test/xml/test.xml"; // long startTime = System.nanoTime();
为什么 SAXParseException 对 getSystemId() 返回 null?什么是系统标识符? import java.io.StringReader; import
本文整理了Java中net.sf.saxon.query.XQueryFunction.getSystemId()方法的一些代码示例,展示了XQueryFunction.getSystemId()的具
本文整理了Java中com.ctc.wstx.evt.WEntityDeclaration.getSystemId()方法的一些代码示例,展示了WEntityDeclaration.getSystem
本文整理了Java中org.deegree.commons.xml.stax.XMLStreamReaderWrapper.getSystemId()方法的一些代码示例,展示了XMLStreamRea
我是一名优秀的程序员,十分优秀!