i have problem, i want get all data from my xml file in many tags name. but when i loop, just get one in first tag. sory my english.
this is my code and sample format xml.
Code:
$xml = @simplexml_load_file('C://xampp/htdocs/logxml.xml');
foreach ($xml->children() as $b){
echo $MakeAndModel = $b->{'xdm:ComponentGroup'}->{'xdm:Component'}->{'dd:DigitalStorageTypeEnum'};
}
<xdm:ComponentGroup>
<xdm:Component id="dev1300" componentType="digitalStorage">
<dd:DigitalStorageTypeEnum>harddisk</dd:DigitalStorageTypeEnum>
<dd:Capacity>
<dd:MaxCapacity>16</dd:MaxCapacity>
<dd:Unit>bytes</dd:Unit>
</dd:Capacity>
</xdm:Component>
</xdm:ComponentGroup>
<xdm:Component id="dev1350" componentType="digitalStorage">
<dd:DigitalStorageTypeEnum>ram</dd:DigitalStorageTypeEnum>
<dd:Capacity>
<dd:MaxCapacity>1040187392</dd:MaxCapacity>
<dd:Unit>bytes</dd:Unit>
</dd:Capacity>
</xdm:Component>
output = hardisk, no one else
and below of my real xml file still have many same tags name that i want to get the value. but it doesn't work. anyone help please.
There are a couple of problems, the main one is being hidden by your use of @
which is hiding any problems it may have loading the XML file. I've corrected the file so that it is now...
<?xml version="1.0" encoding="UTF-8"?>
<xdm:ComponentGroup xmlns:xdm="http://www.hp.com/schemas/imaging/con/xdm/1.1/"
xmlns:dd="http://www.hp.com/schemas/imaging/con/dictionaries/1.0/">
<xdm:Component id="dev1300" componentType="digitalStorage">
<dd:DigitalStorageTypeEnum>harddisk</dd:DigitalStorageTypeEnum>
<dd:Capacity>
<dd:MaxCapacity>16</dd:MaxCapacity>
<dd:Unit>bytes</dd:Unit>
</dd:Capacity>
</xdm:Component>
<xdm:Component id="dev1350" componentType="digitalStorage">
<dd:DigitalStorageTypeEnum>ram</dd:DigitalStorageTypeEnum>
<dd:Capacity>
<dd:MaxCapacity>1040187392</dd:MaxCapacity>
<dd:Unit>bytes</dd:Unit>
</dd:Capacity>
</xdm:Component>
</xdm:ComponentGroup>
I've added a few dummy url's for the namespaces, but there are probably legitimate ones you can use for your case.
Then, using XPath to fetch the various components, you can load the file and output it using...
<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );
$xml = simplexml_load_file('NewFile.xml');
$xml->registerXPathNamespace("xdm", "http://www.hp.com/schemas/imaging/con/xdm/1.1/");
$xml->registerXPathNamespace("dd", "http://www.hp.com/schemas/imaging/con/dictionaries/1.0/");
$components = $xml->xpath("//xdm:Component");
foreach ($components as $component){
$digitalStorageTypeEnum = $component->xpath("descendant::dd:DigitalStorageTypeEnum/text()")[0];
echo $component['id']."-". $digitalStorageTypeEnum.PHP_EOL;
}
This registers the namespaces as it references them in the XPath expressions. So it first looks for all <xdm:Component>
elements and then within that it fetches the dd:DigitalStorageTypeEnum
elements. Although the text()
is only one piece of text, xpath
always returns an array, so just take the first result as the content.
This outputs:
dev1300-harddisk
dev1350-ram
bug here:
</xdm:ComponentGroup> (element close)
you have to put it at the bottom of xml
<xdm:ComponentGroup>
<xdm:Component id="dev1300" componentType="digitalStorage">
<dd:DigitalStorageTypeEnum>harddisk</dd:DigitalStorageTypeEnum>
<dd:Capacity>
<dd:MaxCapacity>16</dd:MaxCapacity>
<dd:Unit>bytes</dd:Unit>
</dd:Capacity>
</xdm:Component>
<xdm:Component id="dev1350" componentType="digitalStorage">
<dd:DigitalStorageTypeEnum>ram</dd:DigitalStorageTypeEnum>
<dd:Capacity>
<dd:MaxCapacity>1040187392</dd:MaxCapacity>
<dd:Unit>bytes</dd:Unit>
</dd:Capacity>
</xdm:Component>
</xdm:ComponentGroup>