dotwc62080
2018-01-22 19:13
浏览 63

Array to xml - 重复标记的问题

EDITED
I'm trying to put my form inputs into an xml file.

Searching on this site I've found the following code and I used it to parse $_POST content.

After a few attempts I realized that "numeric tags" (resulting from not-associative arrays) could be reason of my insuccess so I modified the code as below:

function array_to_xml(array $arr, SimpleXMLElement $xml, $NumK = false)
{
    foreach ($arr as $k => $v) {
        if (is_array($v)){
            preg_match('/^0|[1-9]\d*$/', implode(array_keys($v)))
            ? array_to_xml($v, $xml->addChild($k), true)
            : array_to_xml($v, $xml->addChild($k));
        }else{
            $NumK
            ? $xml->addChild('_'.$k.'_', $v)
            : $xml->addChild($k, $v);
        }
    }
    return $xml;
}

Anyway I'm still "fighting" with xpath commands because I'm not able to find the GrandParent of some nodes (coming from not-associative arrays) that I need to convert into repeated tags.

That's the logic I'm trying to follow:
1st - Find nodes to reformat (The only ones having numeric tag);
2nd - Find grandparent (The tag I need to repeat);
3rd - Replace the grandparent (and his descendants) whith a grandparent's tag for each group of grandchilds (one for each child).

So far I'm still stuck on 1st step beacuse of xpath misunderstanding.

Below, the result xml I have and how I would to transform it:

My array is something like:

$TestArr = Array
    ("First" => array
        ("Martha" => "Text01"
        ,
        "Lucy" => "Text02"
        ,
        "Bob" => array 
            ("Jhon" => array
                ("01", "02")
            ),
        "Frank" => "One"
        ,
        "Jessy" => "Two"
        )
    ,
    "Second" => array
        ("Mary" => array 
            ("Jhon" => array
                ("03", "04")
            ,
            "Frank" => array
                ("Three", "Four")
            ,
            "Jessy" => array
                ("J3", "J4")
            )
        )
    );

using the function array_to_xml($TestArr, new SimpleXMLElement('<root/>')) I get an xml like:

<root>
    <First>
        <Martha>Text01</Martha>
        <Lucy>Text02</Lucy>
        <Bob>
            <Jhon>
                <_0_>01</_0_>
                <_1_>02</_1_>
            </Jhon>
        </Bob>
        <Frank>One</Frank>
        <Jessy>Two</Jessy>
    </First>
    <Second>
        <Mary>
            <Jhon>
                <_0_>03</_0_>
                <_1_>04</_1_>
            </Jhon>
            <Frank>
                <_0_>Three</_0_>
                <_1_>Four</_1_>
            </Frank>
            <Jessy>
                <_0_>J3</_0_>
                <_1_>J4</_1_>
            </Jessy>
        </Mary>
    </Second>
</root>

My needed result is something like:

<root>
    <First>
        <Martha>Text01</Martha>
        <Lucy>Text02</Lucy>
            <Bob>
                <Jhon>01</Jhon>
            </Bob>
            <Bob>
                <Jhon>02</Jhon>
            </Bob>
        <Frank>One</Frank>
        <Jessy>Two</Jessy>
    </First>
    <Second>
        <Mary>
            <Jhon>03</Jhon>
            <Frank>Three</Frank>
            <Jessy>J3</Jessy>
        </Mary>
        <Mary>
            <Jhon>04</Jhon>
            <Frank>Four</Frank>
            <Jessy>J4</Jessy>
        </Mary>
    </Second>
</root>

2条回答 默认 最新

相关推荐 更多相似问题