I would like to get name of Tilt and its value 30. I am not able to get the value of Tilt. Let me know if you have any idea.

Thanks in advanced.

XML File:

<?xml version="1.0" encoding="UTF-8"?>
-<Physical> 
<Catalog> 
</Catalog> 
-<Installed> 
-<Equipment> 
<Id>26</Id>
<Ref>Tew12</Ref>
-<Characteristic>
    <CharacteristicName>Height</CharacteristicName>
    <CharacteristicValue>160</CharacteristicValue>
 </Characteristic> 
 -<Characteristic>
     <CharacteristicName>Tilt</CharacteristicName>
     <CharacteristicValue>30</CharacteristicValue>
</Equipment> 
</Installed> 
</Physical> 

Expected Result:
Id 26
Tilt 30

Code:

/* For Id */
$Idquery = $xml->xpath("/Physical/Installed/Equipment/Id");
foreach ($Idquery as $Id)
{
   print($Idquery->nodeValue);
}
/* For Tilt */
$XMLResults = $xml->xpath("/Physical/Installed/Equipment");
foreach($XMLResults as $item) 
{
    $Tilt = $item->xpath('//Characteristic[@CharacteristicName="Tilt"]');
    print($Tilt->nodeValue);
}

Recommended Answers

All 8 Replies

Try:

$node = $item->xpath('//Characteristic');
if ($node->CharacteristicName == 'Tilt')
    echo $node->CharacteristicValue;

@pritaeas:

It gives null value. It is not working. Do you have other idea? You may modify/change the code.

<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<Physical> 
 <Catalog>
 </Catalog>
 <Installed>
  <Equipment>
   <Id>26</Id>
   <Ref>Tew12</Ref>
   <Characteristic>
    <CharacteristicName>Height</CharacteristicName>
    <CharacteristicValue>160</CharacteristicValue>
   </Characteristic>
   <Characteristic>
     <CharacteristicName>Tilt</CharacteristicName>
     <CharacteristicValue>30</CharacteristicValue>
   </Characteristic>
  </Equipment>
 </Installed>
</Physical>';

$simpleXml = new SimpleXMLElement($xml);

$nodes = $simpleXml->xpath("/Physical/Installed/Equipment/Characteristic");
foreach ($nodes as $node)
{
    if ($node->CharacteristicName == 'Tilt')
    {
        echo $node->CharacteristicValue;
    }
}
?>

@pritaeas: It is very big file. I had used DOM parser. It is displaying the value of tilt. Can you tell me how i can store value of Tilt to insert into the table ?

`// Not inserting value in the table

$nodes = $xml->xpath("/Physical/Installed/Equipment/Characteristic");
foreach ($nodes as $node)
{  
    if ($node->CharacteristicName == 'Tilt')
    {        
        echo $node->CharacteristicValue;
    }
}

I had used DOM parser

Next time say so in your question.

Not inserting value in the table

Not sure what you mean by this.

Not sure what you mean by this.

It is noted. I will mentioned. I mean to say it is printing the Tilt Value 30. But i want to insert this Tilt value in my database table. I think i need to store this value in the varibale and insert into the table.

I hope i am clear to you.

echo $node->CharacteristicValue;

30

But i want to insert this Tilt value in my database table.

Just execute a query. What are you using?

@pritaeas

I just need to store value in a variable and insert it into table. SOLVED. Thanks for your time.

print($Tilt->CharacteristicValue);
$TiltValueInTable =$Tilt->CharacteristicValue;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.