PHP';s SimpleXML不';t保持不同元素类型之间的顺序
据我所知,当您在XML文档树中的同一级别上有多种类型的元素时,PHP的PHP';s SimpleXML不';t保持不同元素类型之间的顺序,php,xml,xml-parsing,simplexml,Php,Xml,Xml Parsing,Simplexml,据我所知,当您在XML文档树中的同一级别上有多种类型的元素时,PHP的SimpleXML,包括SimpleXMLElement和SimpleXMLIterator都不会保持元素之间的顺序,因为它们只在每个元素中相互关联 例如,考虑以下结构: <catalog> <book> <title>Harry Potter and the Chamber of Secrets</title> <author>
SimpleXML
,包括SimpleXMLElement
和SimpleXMLIterator
都不会保持元素之间的顺序,因为它们只在每个元素中相互关联
例如,考虑以下结构:
<catalog>
<book>
<title>Harry Potter and the Chamber of Secrets</title>
<author>J.K. Rowling</author>
</book>
<book>
<title>Great Expectations</title>
<author>Charles Dickens</author>
</book>
</catalog>
这很好,因为我只有book元素,它在这些元素中保持了正确的顺序。但是,假设我也添加了电影元素:
<catalog>
<book>
<title>Harry Potter and the Chamber of Secrets</title>
<author>J.K. Rowling</author>
</book>
<movie>
<title>The Dark Knight</title>
<director>Christopher Nolan</director>
</movie>
<book>
<title>Great Expectations</title>
<author>Charles Dickens</author>
</book>
<movie>
<title>Avatar</title>
<director>Christopher Nolan</director>
</movie>
</catalog>
因为它以这种方式表示数据,所以我似乎无法判断XML文件中书籍和电影的顺序实际上是book,movie,book,movie
。它只是将它们分为两个类别(尽管它在每个类别中保持顺序)
有没有人知道一种变通方法,或者一种不同的XML解析器没有这种行为?“如果我…使用SimpleXMLIterator或SimpleXMLElement来解析它,我会得到一个数组”-不,你不会,你会得到一个对象,它在某些方面恰巧表现得像数组
该对象的递归转储的输出与对其进行迭代的结果不同
特别是,运行foreach($some\u node->children()as$child\u node)
将按照节点在文档中的显示顺序为您提供节点的所有子节点,而不考虑其名称,如中所示
代码:
您可以使用订单注释:
@Root(name="Person")
@Order(elements={"first", "second", "third"})
public class Person {
private String first;
private String second;
private String third;
}
嗯,好的,谢谢!我知道你得到了一个对象,但是当我用
SimpleXMLIterator
解析时,我最终得到了一个数组,因为当我解析时,我把所有的东西都放到了一个数组中。但我一定是做错了什么,因为您的代码不仅非常简单,而且按照我的要求工作。谢谢@JoshSherick您创建的数组结构无法保留项目的顺序,因为结果中只有两个顶级项目:“book”
和“movie”
。这是一个很好的例子,说明了为什么拥有一个SimpleXML对象比试图将整个XML文档表示为一个数组要好——不同的目的需要不同类型的遍历。这是关于名为SimpleXML的PHP模块,与具有类似名称的Java库无关。
Array (
[book] => Array (
[0] => Array (
[title] => Array (
[0] => Harry Potter and the Chamber of Secrets
)
[author] => Array (
[0] => J.K. Rowling
)
)
[1] => Array (
[title] => Array (
[0] => Great Expectations
)
[author] => Array (
[0] => Charles Dickens
)
)
)
[movie] => Array (
[0] => Array (
[title] => Array (
[0] => The Dark Knight
)
[director] => Array (
[0] => Christopher Nolan
)
)
[1] => Array (
[title] => Array (
[0] => Avatar
)
[director] => Array (
[0] => James Cameron
)
)
)
)
$xml = <<<EOF
<catalog>
<book>
<title>Harry Potter and the Chamber of Secrets</title>
<author>J.K. Rowling</author>
</book>
<movie>
<title>The Dark Knight</title>
<director>Christopher Nolan</director>
</movie>
<book>
<title>Great Expectations</title>
<author>Charles Dickens</author>
</book>
<movie>
<title>Avatar</title>
<director>Christopher Nolan</director>
</movie>
</catalog>
EOF;
$sx = simplexml_load_string($xml);
foreach ( $sx->children() as $node )
{
echo $node->getName(), '<br />';
}
book
movie
book
movie
@Root(name="Person")
@Order(elements={"first", "second", "third"})
public class Person {
private String first;
private String second;
private String third;
}