Showing posts with label php Check if Child node exists and get parent node in xml. Show all posts
Showing posts with label php Check if Child node exists and get parent node in xml. Show all posts

Check if Child node exists and get parent node in xml

Wednesday, November 4, 2015

I have loaded a xml file and I want a specific node when a child node exists.



For example: if the supplierItem->supplier->id is "0101" I want to get the stock "1" out of this specific supplierItem. So I need the parent stock node of the specific supplier id.



My xml looks something like this



<product>
<supplierItem>
<id>0001</id>
<supplier>
<id>0101</id>
<name>Company_1</name>
</supplier>
<supplierItemId>Product_1_ID</supplierItemId>
<productName>Product_1</productName>
<stock>1</stock>
</supplierItem>
<id>0002</id>
<supplier>
<id>0202</id>
<name>Company_2</name>
</supplier>
<supplierItemId>Product_1_ID</supplierItemId>
<productName>Product_1</productName>
<stock>2</stock>
</supplierItem>
<id>0003</id>
<supplier>
<id>0303</id>
<name>Company_3</name>
</supplier>
<supplierItemId>Product_1_ID</supplierItemId>
<productName>Product_1</productName>
<stock>3</stock>
</supplierItem>
</product>


My first attemp were:



 $stocks = $xml->xpath("/product/supplierItem");
foreach ($stocks as $stock)
{
echo($stock); // get all stocks
}


Any idea? I stuck with it...
Thanks in advance

Check if Child node exists and get parent node in xml

I have loaded a xml file and I want a specific node when a child node exists.



For example: if the supplierItem->supplier->id is "0101" I want to get the stock "1" out of this specific supplierItem. So I need the parent stock node of the specific supplier id.



My xml looks something like this



<product>
<supplierItem>
<id>0001</id>
<supplier>
<id>0101</id>
<name>Company_1</name>
</supplier>
<supplierItemId>Product_1_ID</supplierItemId>
<productName>Product_1</productName>
<stock>1</stock>
</supplierItem>
<id>0002</id>
<supplier>
<id>0202</id>
<name>Company_2</name>
</supplier>
<supplierItemId>Product_1_ID</supplierItemId>
<productName>Product_1</productName>
<stock>2</stock>
</supplierItem>
<id>0003</id>
<supplier>
<id>0303</id>
<name>Company_3</name>
</supplier>
<supplierItemId>Product_1_ID</supplierItemId>
<productName>Product_1</productName>
<stock>3</stock>
</supplierItem>
</product>


My first attemp were:



 $stocks = $xml->xpath("/product/supplierItem");
foreach ($stocks as $stock)
{
echo($stock); // get all stocks
}


Any idea? I stuck with it...
Thanks in advance