xml 中的 DTD 如何使使用 java DOM 更容易解析?
嘿朋友们, 我被分配了一个查询规划项目。 在这个项目中,如果输入类似 sql 的查询,我必须对其进行切片并转换为 xml。 我做了这一部分,但是我需要为此 xml 添加 DTD,因为正如项目提到的那样,它有助于解析(使用 java DOM)此查询并轻松找到选择 - 查询中指定的重复和联接。
我不明白,在使用 DOM 解析 xml 并找到该 xml 的不同部分时,DTD 如何提供帮助?
我可以使用 DOM 来查找 xml 的不同部分,而无需 dtd...任何人都可以给我提供差异的示例吗?
谢谢
hey friends,
i have been assigned a query planning project.
in this project if enter an sql like query, which i have to slice and turn into an xml.
i did this part, however i am required to add a DTD for this xml, because as the project mentions it helps to parse (using java DOM) this query and find easily the selection - duplicates and joins specified in the query.
i don't understand, how does a DTD help while using DOM to parse the xml and find the different parts of this xml?
i could use DOM to find the different parts of the xml without a dtd... Can anybody give me and example of the difference?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DTD 告诉解析器哪些标签是允许的,以及它们应该出现在文档中的什么位置。如果没有 DTD,解析器将读取标签,但不知道该标签是否是预期的标签,或者它是否位于正确的位置。
如果您使用 SAX 或 DOM 解析 XML,这并不重要,如果没有 DTD(或者最近的替代品之一,如 XSD、RelaxNG 等),解析器都不会知道您的标记是预期的还是意外的。
A DTD tells the parser which tags are allowed, and where in the document they should be expected. Without a DTD, the parser will read the tags but it won't know if the tag was an expected one, or if it was in the right place.
If you parse your XML with SAX or DOM, it doesn't matter, neither parser will know if your tags are expected or unexpected without a DTD (or one of it's more recent replacements like XSD, RelaxNG, etc).
DTD 不帮助解析 XML,但它通过定义有关文档的某些规则来提供基本验证。如果文档违反验证规则,则解析应该失败(或生成警告消息,这应该取决于验证器配置)。他们可能称其“有用”,因为您的 DOM 导航代码将能够对文档的结构做出更好的假设,而不必担心不恰当的失败。
他们可能希望您包含 DTD,因为否则必须从 PUBLIC/SYSTEM 文档标识符识别 DTD,并将其托管在某处。或者他们应该在源代码中具有预定义的 DTD(也是“托管某处”的一个版本),如果相同的代码必须在不事先了解结构的情况下处理不同的文档,则这可能不是一个选项。
关于更容易查找重复项的说法可能是因为他们计划在出现重复项时将其丢弃;在不了解切片和车削细节的情况下,不确定它对连接有多大帮助。
DTD does not help to parse the XML, but it provides a rudimentary validation by defining certain rules about the document. If the document violates the validation rules, the parsing should fail (or produce a warning message, that should depend on the validators configuration). They may call it "helpful", because your DOM navigation code would be able to make better assumptions about the structure of the document, without fear of ungraceful failure.
They probably want you to include the DTD because otherwise the DTD has to be recognized from PUBLIC/SYSTEM document identifier, and be hosted somewhere. Or they should have a predefined DTD in the source code(also a version of this "hosting somewhere"), which may not be an option if the same code has to process different documents without prior knowledge to the structure.
The statement about finding duplicates easier may be because they plan to throw the document out in case of a duplicate; not sure how helpful it is for the joins, not without knowing the details of the slicing and turning.