找出差异!

发布于 2024-09-18 13:47:51 字数 2070 浏览 5 评论 0 原文

我需要快速帮助来解决一个棘手的问题,这个问题简直让我发疯。

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 个不同的结果... 我会尝试区分它们...

I need a quick help for a tricky problem that is literally driving me crazy.

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>";

The following code to get a w3c.dom.Document object

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

The first string (example) is parsed correctly, the second one (example2) returns null.

Any idea why? I can't really see any difference between the 2!!

Thanks in advance I'm sure it's something so dumb I will feel ashamed...

EDIT: actually with the same content in the string, still 2 different outcome...
I'll try to diff them...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

朱染 2024-09-25 13:47:51

你检查过看不见的字符吗?我过去发现 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.

日裸衫吸 2024-09-25 13:47:51

您在第二个的 note 标记中缺少

You're missing a </src> in the note tag in the second one.

回眸一笑 2024-09-25 13:47:51

让计算机完成工作: 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.

素罗衫 2024-09-25 13:47:51

这次可能没有帮助,但我通常将 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.

爱人如己 2024-09-25 13:47:51

我知道我会感到羞耻。

拼写错误,其中一个为 archiveDate,另一个为 archiveialDate。

请现在埋葬我...

I knew I would be ashamed.

Spelling error, archivalDate in one and archivialDate in the other.

Plese bury me now...

萌吟 2024-09-25 13:47:51

嘿!你是说第二个(example2)你确定你正在解析example4吗?而不是名为 example2 的东西,它可能为空?

我测试了你的代码,没有发现任何异常。
否则,我很确定您正在处理第二个 xml 中不存在的某个节点 - example4 - 例如 archivalDate

Hey! you are saying the second one (example2) are you sure you are parsing example4? and not something called example2 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 as archivalDate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文