找出差异!
我需要快速帮助来解决一个棘手的问题,这个问题简直让我发疯。
String example = "<digitalObject>" +
"<title>title</title>" +
"<creator>Name</creator>" +
"<location>link</location>"+
"<relatedAsset>related realife object</relatedAsset>" +
"<note><src lang =\"en\">value</src></note>" +
"<archivalDate>date</archivalDate>"+
"<mimeFormat>mime type</mimeFormat>"+
"<digitalObjectOwner>owner</digitalObjectOwner>"+
"</digitalObject>";
String example4="<digitalObject>" +
"<title>title</title>"+
"<creator>name</creator>"+
"<location>link</location>"+
"<relatedAsset>related realife object</relatedAsset>" +
"<note><src lang=\"en\">value</src></note>" +
"<archivialDate>date</archivialDate>"+
"<mimeFormat>mime type</mimeFormat>" +
"<digitalObjectOwner>owner</digitalObjectOwner>" +
"</digitalObject>";
以下代码获取 w3c.dom.Document 对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document doc=null;
try {
builder = factory.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(example4));
doc = builder.parse(is);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return doc;
}
第一个字符串(example)被正确解析,第二个字符串(example2)返回 null。
知道为什么吗?我真的看不出两者之间有什么区别!
提前致谢,我确信这是如此愚蠢的事情,我会感到羞愧...
编辑:实际上字符串中的内容相同,但仍然有 2 个不同的结果... 我会尝试区分它们...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你检查过看不见的字符吗?我过去发现 xml 中存在与我期望发送的内容不同的不可见字符。
Have you checked for invisible characters? I have found in the past that there are invisible characters in an xml that are different from what I am expecting to have sent.
您在第二个的
note
标记中缺少。
You're missing a
</src>
in thenote
tag in the second one.让计算机完成工作: http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html#difference(java.lang.String, java.lang.String )
如果您期望结构相同但内容不同,则您应该只在输出中看到内容更改。
Let the computer do the work: http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html#difference(java.lang.String, java.lang.String)
If you expect the structure to be the same, but different contents, you should only see the content changes in the output.
这次可能没有帮助,但我通常将 XML 粘贴到 Eclipse 中的 *.xml 文件中,然后自动格式化该文件 (ctrl-shift-f),这使得类似的事情变得非常非常容易观察。
Might not help this time, but I usually paste XML into an *.xml file in Eclipse, then auto-format the file (ctrl-shift-f), which makes things like this much, much easier to eyeball.
我知道我会感到羞耻。
拼写错误,其中一个为 archiveDate,另一个为 archiveialDate。
请现在埋葬我...
I knew I would be ashamed.
Spelling error, archivalDate in one and archivialDate in the other.
Plese bury me now...
嘿!你是说
第二个(example2)
你确定你正在解析example4
吗?而不是名为example2
的东西,它可能为空?我测试了你的代码,没有发现任何异常。
否则,我很确定您正在处理第二个 xml 中不存在的某个节点 -
example4
- 例如archivalDate
Hey! you are saying
the second one (example2)
are you sure you are parsingexample4
? and not something calledexample2
which could be null?I tested your code, and I did not get any exception.
If otherwise, I am pretty sure you are dealing with some node, that does not exist in second xml -
example4
- such asarchivalDate