0

I have a soap xml that contains a bunch of variables that I need to access. Here is the XML.

`<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <searchPersonsResponse xmlns="">
         <searchPersonsReturn>
            <attributes>
               <attributes>
                  <name>ercreatedate</name>
                  <values>
                     <values>201104070130Z</values>
                  </values>
               </attributes>
               <attributes>
                  <name>status</name>
                  <values>
                     <values>Stuff1</values>
                     <values>Stuff2</values>
                     <values>Stuff3</values>
                     <values>Stuff4</values>
                     <values>Stuff5</values>
                     <values>Stuff6</values>
                     <values>Stuff7</values>
                  </values>
               </attributes>
           </attributes>

            <itimDN>blah</itimDN>
            <name>Smith, Bob</name>
            <profileName>PER</profileName>
            <select>false</select>
         </searchPersonsReturn>
      </searchPersonsResponse>
   </soapenv:Body>
</soapenv:Envelope>

I'm trying to access the inner attribute node and pull out the Name and values into a multidimentional array like this ....

$array["status"][0]="stuff1";
$array["status"][1]="stuff2";
$array["status"][2]="stuff3";
$array["status"][3]="stuff4";

so far I have been able to access the nodes but not really get them the way I want. here is the code I have been playing around with .....

$dom_document = new DOMDocument();

$dom_document->loadXML($thexml);

$tag_els_names = $dom_document->getElementsByTagname('name');
$tag_els_values = $dom_document->getElementsByTagname('values');

$data = array();
$data2 = array();
foreach($tag_els_names as $node){
    $data[] = array($node->nodeName => $node->nodeValue);
//grabs all the <name> node values
}
$i=0;$j=0;
foreach($tag_els_values as $node){
    $j=0;


    foreach($node->childNodes as $child) {


            $data2[$i][$j] = $child->nodeValue;
                     //grabs all the value node values    
        $j++;
    }
    $i++;

    $j=0;
}

Does anyone know an easy way to do this? I think that I have been looking at this for way to long.

4

2 回答 2

0

怎么样:

$dom_document = new DOMDocument();

$dom_document->loadXML($thexml);

$xpath = new DOMXpath($dom_document);

$attr = $xpath->evaluate("//attribute/attributes");

$names = array();
$values = array();
$i = 0;
foreach($attr as $attr_node) {
    $values[i] = array();
    foreach($xpath->evaluate("name", $attr_node) as $name){
        $names[] = $name->nodeValue;
    }
    $foreach($xpath->evaluate("value", $attr_node) as $value){
        $values[i][] = $value->nodeValue;
    }
    i++;
}

但是,这会错过组<name>外的元素<attributes>。你的意思是包括那个?

于 2013-02-02T01:41:18.770 回答
0

我想通了,并认为它可以帮助别人

    $doc = new DOMDocument();
    $values=array();

    if ($doc->loadXML($temp)) {
        $attributes = $doc->getElementsByTagName('attributes');

        foreach($attributes as $attribute) {
            if($attribute->childNodes->length) {
                $previous_nodeValue="";
                    foreach($attribute->childNodes as $i) {
                        if($i->nodeValue=="status"){
                            $previous_nodeValue=$i->nodeValue;
                        }
                        if($i->nodeName=="values" && $previous_nodeValue== "status"){

                            foreach($i->childNodes as $j){
                                $values[]=$j->nodeValue;

                            }
                        }
                    }
                $previous_nodeValue="";
            }
         }

     }  
于 2013-02-05T17:13:49.113 回答