Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

sort xml div by child node PHP SimpleXML

so I have a list of <div>'s in an xml file. I'm parsing the file using php's simpleXML

I can generate an array of all the divs with the following:

$divArray = $xmldoc->text->body->children();

But now I would like to order the $divArray by different childNodes (author, title, date) within the div.

The div looks like this.

<div>
    <bibl>
       <author>
       <title>
       <date>
   </bibl>
</div>

So how can I take $divArray and sort it by <author> or <title> or <date>?

Thanks for your help. jw

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The basic procedure is

  1. cast a SimpleXMLElement into an array
  2. write a comparison function that accepts two SimpleXMLElement arguments
  3. sort the array with the comparison function using usort()

I can only guess at your original XML structure, but I think it looks something like this:

$xml = <<<EOT
<root>
<text>
    <body>
        <div>
            <bibl>
                <author>A</author>
                <title>A</title>
                <date>1</date>
            </bibl>
        </div>
        <div>
            <bibl>
                <author>B</author>
                <title>B</title>
                <date>2</date>
            </bibl>
        </div>
        <div>
            <bibl>
                <author>D</author>
                <title>D</title>
                <date>4</date>
            </bibl>
        </div>
        <div>
            <bibl>
                <author>C</author>
                <title>C</title>
                <date>3</date>
            </bibl>
        </div>
    </body>
</text>
</root>
EOT;

$xmldoc = new SimpleXMLElement($xml);

Step 1: Cast to array. Note that your $divArray is not actually an array!

$divSXE = $xmldoc->text->body->children(); // is a SimpleXMLElement, not an array!
// print_r($divSXE);
$divArray = array();
foreach($divSXE->div as $d) {
    $divArray[] = $d;
}
// print_r($divArray);

Step 2: write a comparison function. Since the array is a list of SimpleXMLElements, the comparison function must accept SimpleXMLElement arguments. SimpleXMLElements need explicit casting to get string or integer values.

function author_cmp($a, $b) {
    $va = (string) $a->bibl->author;
    $vb = (string) $b->bibl->author;
    if ($va===$vb) {
        return 0;
    }
    return ($va<$vb) ? -1 : 1;
}

Step 3: Sort the array with usort()

usort($divArray, 'author_cmp');
print_r($divArray);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...