如何从具有未知格式的XML文件中读取,例如文件的格式可以是:
<data1>int1</data1> <data2>int2</data2> <data3>int3</data3> <data4>int4</data4>要么
<data1>int1</data1> <data4>int4</data4>要么
<data1>int1</data1> <data2>int2</data2> <data3>int3</data3> <data4>int4</data4> <data5>int5</data5>在第二种情况下,我假设int2和int3将被分配默认值。 我想到了解决这个问题的一种方法,但它出现了凌乱和意大利面。 任何帮助,将不胜感激!
How do I read from an XML file with an unkown format, for example a file can be of the format:
<data1>int1</data1> <data2>int2</data2> <data3>int3</data3> <data4>int4</data4>OR
<data1>int1</data1> <data4>int4</data4>OR
<data1>int1</data1> <data2>int2</data2> <data3>int3</data3> <data4>int4</data4> <data5>int5</data5>In the second case I am to assume int2 and int3 are to be assigned default values. I thought of one way to solve this problem but it came out messy and spaghetti like. Any help would be appreciated!
最满意答案
(1)这些都不是完整的XML文档。 需要有一个根元素。
(2)如果您不坚持限制其实例的数量和顺序,则很容易在DTD中声明元素内容可以是其他元素的混合,使用“|” 运营商。 (见http://www.w3.org/TR/xml11/#sec-element-content )
(3)如果你想更严格地约束那些,那么是的,DTD可以要求拼写出所有的组合。 切换到针对XML Schema的验证是一个显而易见的解决方案; DTD几乎被认为是过时的,因为它们与XML命名空间(它已成为XML处理的基本部分)不兼容。
(4)如果你坚持坚持使用DTD,并且不能接受无约束的顺序/计数,并且不想拼出所有可能的顺序......考虑做一些检查和/或应用默认值您的应用程序代码而不是DTD。
(1) None of those is a complete XML document. There needs to be a single root element.
(2) If you don't insist on restricting the number and order of their of instances, it's easy to declare in a DTD that element content can be a mix of other elements, using the '|' operator. (See http://www.w3.org/TR/xml11/#sec-element-content)
(3) If you want to constrain those more tightly, then yes, DTDs can require spelling out all the combinations. Switching to validating against XML Schemas is one obvious solution; DTDs are pretty much considered obsolete anyway since they aren't compatible with XML Namespaces (which have become a basic part of XML processing).
(4) If you insist on sticking with DTDs, and can't accept unconstrained order/count, and don't want to spell out all the possible sequences... consider doing some of that checking and/or applying of default values in your application code rather than in the DTD.
更多推荐
int,XML,<data,>int,电脑培训,计算机培训,IT培训"/> <meta name="desc
发布评论