0

I have an xml witch contains value and children element as you can see below:

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

And i have to get both the values and children elements in order:

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

The main goal is to be able to put this back to the xml file in the correct order, after we changed the values.

If you want to see more accurate example here is a code on top with the xml and below with the php counter part of it:

<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' => []
                ],
            ],
        ]
    ];
Twois
  • 548
  • 1
  • 7
  • 18
  • show us your efforts, pls. – michi Feb 18 '16 at 20:13
  • I want to know how to work with xml in that case when the xml looks like this: – Twois Feb 18 '16 at 21:22
  • [edited]: I want to know how to work with xml in that case when the xml looks like this: "some text text two other text". so i want to convert it to array (because we have large xmls and we need to edit them on a web page, but we don't want to parse the file every time so we want to save in database (mongoDB)). And also we need to convert the array back with new values. – Twois Feb 18 '16 at 21:29
  • If you need a library to work with XLIFF in PHP please have a look at the answers [here](http://stackoverflow.com/questions/10044649/php-library-for-xliff). Do not forget to vote for the answer you found useful. And if not, please improve your question and write down how you tried to get from XML to your array, and what did not work for you. By the way, if your example is supposed to be XLIFF 1.2 it is invalid: the `rk` element should be `mrk`. – Jenszcz Feb 19 '16 at 11:17
  • @Twois see http://stackoverflow.com/questions/6399924/getting-nodes-text-in-php-dom – michi Feb 19 '16 at 11:41
  • @michi, thank you, this solution might help me! – Twois Feb 19 '16 at 11:48

0 Answers0