2010-09-28 20 views
6

Sto attraversando un file di definizione xml e ho un DOMNodeList che sto attraversando. Ho bisogno di estrarre il contenuto di un tag figlio che può o non può essere in entità correnteOttieni un tag figlio specifico da un DOMElement in PHP

<input id="name"> 
    <label>Full Name:</label> 
    <required /> 
</input> 
<input id="phone"> 
    <required /> 
</input> 
<input id="email" /> 

Ho bisogno di sostituire ????????????? con qualcosa che mi ottiene il contenuto del tag etichetta se esiste .

Codice: Risultato

foreach($dom->getElementsByTagName('required') as $required){ 
    $curr = $required->parentNode; 

    $label[$curr->getAttribute('id')] = ????????????? 
} 

atteso:

Array(
    ['name'] => "Full Name:" 
    ['phone'] => 
) 

risposta

8

cosa strana è: sai già la risposta dal momento che lo avete usato nello script, getElementsByTagName().
Ma questa volta non con il DOMDocument come contesto "nodo", ma con la input DOMElement:

<?php 
$doc = getDoc(); 
foreach($doc->getElementsByTagName('required') as $e) { 
    $e = $e->parentNode; // this should be the <input> element 
    // all <label> elements that are direct children of this <input> element 
    foreach($e->getElementsByTagName('label') as $l) { 
    echo 'label="', $l->nodeValue, "\"\n"; 
    } 
} 

function getDoc() { 
    $doc = new DOMDocument; 
    $doc->loadxml('<foo> 
    <input id="name"> 
     <label>Full Name:</label> 
     <required /> 
    </input> 
    <input id="phone"> 
     <required /> 
    </input> 
    <input id="email" /> 
    </foo>'); 
    return $doc; 
} 

stampe label="Full Name:"

+0

rock, che lo farà, grazie! –

Problemi correlati