0

I am currently attempting to extract values from an array that was generated by an SIMPLEXML/XPATH Query. I have had no success with my attempts if anyone can take look would be greatly appreciated.

I am looking just to extract ReclaimDate. I have tried a few functions and some of the advice on this post with no luck.

Array
(
[0] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [ReclaimDate] => 05/15/2008
                        [ReclaimPrice] => 555555555
                        [_Owner] => ownername
                    )

            )

    )
2
  • You're probably looking for something like (string)$myvar[0][0]['ReclaimDate'] -- if you could provide a short example doc and sample code, it'd be easier to provide a solution that's guaranteed to work. BTW the SO post you linked appears to not be about SimpleXML, so it's probably not helpful. Commented Jun 14, 2011 at 2:58
  • 1
    can you post the response data (or URL to it) and/or the PHP you're using to get this data as a SimpleXMLElement? Commented Jun 14, 2011 at 2:59

2 Answers 2

1

If I just had to take a stab, I'd agree that what @Frank Farmer said should work:

    // if $myVar is what you print_r'ed
    echo (string)$myVar[0][0]['ReclaimDate'];

or this

    echo (string)$myVar[0][0]->attributes('ReclaimDate');

http://www.php.net/manual/en/simplexml.examples-basic.php#example-4587

Sign up to request clarification or add additional context in comments.

Comments

0

This is resolved thanks to Frank Farmer and Dan Beam.

This worked : echo (string)$check_reo[0][0]['ReclaimDate']

For anyone that is looking to use SimpleXML and XPATH to extract and write some basic logic from an XML file this is what worked for me.

$xmlstring = <<<XML <?xml version='1.0' standalone='yes'?> <YOURXMLGOESHERE>TEST</YOURXMLGOESHERE> XML;

$xpathcount = simplexml_load_string($xmlstring); // Load XML for XPATH Node Counts

$doc = new DOMDocument(); // Create new DOM Instance for Parsing

$xpathcountstr = $xpathcount->asXML(); // Xpath Query

$doc->loadXML($xpathcountstr); // Load Query Results

$xpathquery = array($xpathcount->xpath("//XMLNODEA[1]/XMLNODEB/*[name()='KEYWORDTOCHECKIFXMLCEXISTS']"));

print_r ($xpathquery) // CHECK Array that is returned from the XPATH query

`Array ( [0] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [ReclaimDate] => 05/15/2008 [ReclaimPrice] => 555555555 [_Owner] => ownername )

        )

) // Array RETURNED`

echo (string)$xpathquery[0][0]['ReclaimDate'] // EXTRACT THE VALUE FROM THE ARRAY COLUMN;

This site helped me receive a better understanding on how XPATH can search XML very easily with a lot more features than what I had previously known.

http://zvon.org/xxl/XPathTutorial/Output/examples.html

Here is the simple XML Function that worked for me

$xmlread = simplexml_load_string($xmlstring, "simple_xml_extended");

`class simple_xml_extended extends SimpleXMLElement { // Read XML and get attribute public function Attribute($name){ foreach($this->Attributes() as $key=>$val) { if($key == $name) return (string)$val;

    }
}

}`

Here is the Function action when extracting Single Values with an attribute based XML results

$GETVAR1 = $xmlread->XMLNODE1->XMLNODE2->XMLNODE3->XMLNODE4->XMLNODE5[0]->Attribute('XMLNODE5ATTRIBUTE');

This might not be the most efficient or best method, but its what ended working our for me. Hope this helps someone out who is still unclear about SIMPLEXML and XPATH.

An additional link for further insight: http://www.phpfreaks.com/tutorial/handling-xml-data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.