Jim
Jim

Reputation: 923

xpath select and save specific index

i have an xml parser in my php (simplexml) and wanna select and save an index element under xpath

function startXML(&$resarrary, $path, $lang) {

if(file_exists($path)) {
    $xml = simplexml_load_file($path);
    if($xml) {      
        foreach($xml->xpath("//languageKey") as $cos) {
        utf8_encode($cos);

        // Select required languagekey 

        if ($cos[index] == $lang) {
        foreach($xml->xpath("//languageKey[@index='$lang']//label[@type='js']") as $cos) {
            ?>
                <table>
                    <tr>
                        <td><?php echo $cos ?></td>
                    </tr>
                </table>    
            <?php 
            array_push($resarrary,$cos);

            }

        } 

      }

    } 
    else {
        FALSE;
    }
}

}

the table echo $cos is just for testing....it gives me the correct result of my search from my xml file (in this case the result is "Mode 1")

so the selection works but how can i can save the result ("Mode 1") to an array? If i try this array_push(...) and use print_r for viewing the resarray i only get the whole content of the simplexml xpath search

Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [index] => list_mode_1 [type] => js ) [0] => Mode 1 ) )

If i change the line array_push($resarrary,$cos->attributes()); i get this

Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [index] => list_mode_1 [type] => js ) ) )

Upvotes: 1

Views: 325

Answers (1)

Jim
Jim

Reputation: 923

XML

<lKey index="default" type="array">
    <a index="list_mode_1" type="js">Mode 1</a>
    <b index="list_mode_2">Mode 2</b>
    <c index="list_mode_3">Mode 3</c>
</lKey>

Upvotes: 0

Related Questions