0

I have gone through a lot of the SimpleXML questions on this site. My data is a bit strange and I cannot change that. I am trying to get things like 'Building1' and 'Hostname1' from my data, so I can take that data and look up other data, then display it.

Here is a sample of my data:

    <?xml version='1.0' encoding='UTF-8'?>
<results preview = '0'>
    <result offset='0'>
        <field k='hostname'>
          <value h='1'><text>Hostname 1</text></value>
        </field>
        <field k='os'>
          <value><text>Windows 7</text></value>
        </field>        
        <field k='location'>
          <value h='1'><text>Building 1</text></value>
        <field>
    </result>
   <result offset='1'>
        <field k='hostname'>
          <value h='1'><text>Hostname 2</text></value>
        </field>
        <field k='os'>
          <value><text>Windows 10</text></value>
        </field>        
        <field k='location'>
          <value h='1'><text>Building 2</text></value>
        </field>
     </result>
........

And here is how I am trying to look at it:

$xml = simplexml_load_file(data.xml);
print_r($xml);    
$testArray = new SimpleXMLElement($xml);
$records = $testArray->results->result;
print_r($records);

For some reason I just cannot figure out how to get the data from the xml elements. If anyone can point me in the right direction, I'd appreciate it. I've tried many, many options. Thanks-

user1309220
  • 129
  • 4
  • 15

2 Answers2

0

This is a really common mistake, but a really hard one to spot if you don't know what you're looking for: the first object you get back when parsing with XML is the root element, not something representing the document.

So in your case, $testArray is the element <results preview = '0'>, and you want $testArray->result not $testArray->results->result.

By the way, "testArray" is a bad name for this variable - it's not an array, it's an object.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • That makes sense. However, when I dump $testArray (which I renamed $testObject), I see the following results: ` "object(SimpleXMLElement)#9 (2) { ["@attributes"]=> array(1) {["offset"]=>string(1) "0" } ["field']= array(18) ` So I assume testOject is am XML Element. I try: `$objects = new SimpleXMLElement($testArray->result); ` How do I drill down from here?. – user1309220 Mar 11 '19 at 14:05
  • Sorry, I put backticks around my code, but it did not show up in the answer. – user1309220 Mar 11 '19 at 14:07
  • @user1309220 You don't need `new SimpleXMLElement($testArray->result)`, just `$testArray->result`, which is already an object you can use in a loop, e.g. `foreach ( $testObject->result as $result ) { echo $result['offset']; }`. See [the examples in the manual](http://php.net/manual/en/simplexml.examples-basic.php). – IMSoP Mar 11 '19 at 14:23
  • Ok. I guess I keep getting confused because my xml has "values" such as "field k='hostname'" and other examples don't have that. I am really after the values in the xml. Thanks – user1309220 Mar 11 '19 at 17:02
  • @user1309220 That's an "element" called `field` with an "attribute" called `k`. See examples 5 and 6 in the manual page I linked to. – IMSoP Mar 11 '19 at 17:27
0

I used xml as string in file

<?php
$sXmlString = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<results preview = "0">
    <result offset="0">
        <field k="hostname">
          <value h="1"><text>Hostname 1</text></value>
        </field>
        <field k="os">
          <value><text>Windows 7</text></value>
        </field>        
        <field k="location">
          <value h="1"><text>Building 1</text></value>
        </field>
    </result>
    <result offset="1">
        <field k="hostname">
          <value h="1"><text>Hostname 2</text></value>
        </field>
        <field k="os">
          <value><text>Windows 10</text></value>
        </field>        
        <field k="location">
          <value h="1"><text>Building 2</text></value>
        </field>
    </result>
</results>
EOF;

echo '<pre>';
$xml = simplexml_load_string($sXmlString);
print_r($xml);
echo '<hr/>';
echo count($xml->result);
echo '<hr/>';
foreach($xml->result as $report)
{
    var_dump($report);
    echo '<hr/>';
}

In the code you can see $xml it self reference to the "results" (or root) element. You need to travel from the root to child elements. $xml->result will give the result object in the results set and you need to go for loop as it as array of objects.

balakrishnan
  • 383
  • 4
  • 12