如何从xml和php中按顺序获取值和childlen元素

时间:2016-02-18 18:34:10

标签: php html xml xliff

我有一个xml女巫包含值和children元素,如下所示:

<rk id="1" type="seg">
  some text <g id="11">italic text</g> other text
</rk>

我必须按顺序获取值和子元素:

$arr = [
  'value1' => 'some text ',
  'children' => '<g id="11">italic text</g>',
  'value2' => ' other text'
];

主要目标是在我们更改值后,能够以正确的顺序将其放回xml文件。

如果你想看到更准确的例子,这里的代码是xml及以下的代码,其中包含php计数器部分:

<source>
        <x id="10">
            <rk id="1" type="seg">
                some text<g id="11">italic text</g> other text
            </rk>
        </x>

        <rk id="2">
            text <g id="12">italic text</g>
        </rk>
    </source>*/

    $source = [
        0 => [
            'tag' => 'x',
            'attrs' => [
                0 => [
                    'name' => 'id',
                    'value' => '10'
                ]
            ],
            'childrens' => [
                0 => [
                    'tag' => 'rk',
                    'attrs' => [
                        0 => [
                            'name' => 'id',
                            'value' => '1'
                        ],
                        1 => [
                            'name' => 'type',
                            'value' => 'seg'
                        ]
                    ],
                    'value1' => 'some text ',
                    'childrens' => [
                        0 => [
                            'tag' => 'g',
                            'attrs' => [
                                0 => [
                                    'name' => 'id',
                                    'value' => '10'
                                ]
                            ],
                            'value' => 'italic text',
                            'childrens' => []
                        ],
                    ],
                    'value2' => ' other text',
                ]
            ]
        ],
        1 => [
            'tag' => 'rk',
            'attrs' => [
                0 => [
                    'name' => 'id',
                    'value' => '2'
                ],
                1 => [
                    'name' => 'type',
                    'value' => 'seg'
                ]
            ],
            'value1' => 'text',
            'childrens' => [
                0 => [
                    'tag' => 'g',
                    'attrs' => [
                        0 => [
                            'name' => 'id',
                            'value' => '12'
                        ]
                    ],
                    'value' => 'italic text',
                    'childrens' => []
                ],
            ],
        ]
    ];

0 个答案:

没有答案