1

I am getting XML from a web service, and am having a hard time maintaining the order of the XML elements that are displayed.

Here is a snippet:

    <Blocks>
      <Block>
         <BlockElements>
            <Question QuestionID="QID59" />
            <PageBreak />
            <Question QuestionID="QID3" />
            <Question QuestionID="QID1" />
            <PageBreak />
            <Question QuestionID="QID2" />
            <PageBreak />
            <Question QuestionID="QID47" />
         </BlockElements>
      </Block>
      <Block>
         <BlockElements>
            <Question QuestionID="QID6" />
            <PageBreak />
            <Question QuestionID="QID8" />
            <Question QuestionID="QID49" />
            <Question QuestionID="QID45" />
            <Question QuestionID="QID50" />
            <PageBreak />
            <Question QuestionID="QID9" />
            <Question QuestionID="QID10" />
            <PageBreak />
            <Question QuestionID="QID11" />
            <Question QuestionID="QID12" />
            <PageBreak />
            <Question QuestionID="QID13" />
            <PageBreak />
            <Question QuestionID="QID14" />
            <PageBreak />
            <Question QuestionID="QID15" />
            <PageBreak />
            <Question QuestionID="QID16" />
            <PageBreak />
            <Question QuestionID="QID17" />
            <PageBreak />
            <Question QuestionID="QID18" />
            <Question QuestionID="QID19" />
            <PageBreak />
            <Question QuestionID="QID20" />
            <Question QuestionID="QID21" />
            <PageBreak />
            <Question QuestionID="QID22" />
            <Question QuestionID="QID23" />
         </BlockElements>
      </Block>
    </Blocks>

The web service uses the empty elements and (elements without closing tags). The order of these elements within the BlockElements tags is crucial- it defines the layout of a form. When I turn this XML into a PHP object using a SimpleXmlElement, this is what I get:

SimpleXMLElement Object
(
    [Block] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [BlockElements] => SimpleXMLElement Object
                        (
                            [Question] => Array
                                (
                                    [0] => SimpleXMLElement Object
                                        (
                                            [@attributes] => Array
                                                (
                                                    [QuestionID] => QID59
                                                )
                                        )
                                    [1] => SimpleXMLElement Object
                                        (
                                            [@attributes] => Array
                                                (
                                                    [QuestionID] => QID3
                                                )
                                        )
                                    [2] => SimpleXMLElement Object
                                        (
                                            [@attributes] => Array
                                                (
                                                    [QuestionID] => QID1
                                                )
                                        )
                                    [3] => SimpleXMLElement Object
                                        (
                                            [@attributes] => Array
                                                (
                                                    [QuestionID] => QID2
                                                )
                                        )
                                    [4] => SimpleXMLElement Object
                                        (
                                            [@attributes] => Array
                                                (
                                                    [QuestionID] => QID47
                                                )
                                        )
                                )
                            [PageBreak] => Array
                                (
                                    [0] => SimpleXMLElement Object
                                        (
                                        )
                                    [1] => SimpleXMLElement Object
                                        (
                                        )
                                    [2] => SimpleXMLElement Object
                                        (
                                        )
                                )
                        )
                )
....

The Question elements and PageBreak elements are separated into arrays, and their order is lost. Any advice on maintaining the order of these elements? I am not committed to using SimpleXmlElement- I'll use whatever works. Thanks!

1 Answer 1

1

You have to know the result of iterating of that object is not the same of result of 'print_r' or 'var_dump'

try this

$parentNode = simplexml_load_string($xml);
itNode($parentNode);

function itNode($parentNode){
 if($parentNode->children())
  foreach ( $parentNode->children() as $node )
  {
     echo "name ==> " , $node->getName(),'<br>';
     itNode($node);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.