0

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);
}

Edited by pritaeas: Moved.

2
Contributors
8
Replies
29
Views
4 Years
Discussion Span
Last Post by PriteshP23
0

Try:

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

@pritaeas:

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

0
<?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;
    }
}
?>
0

@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;
    }
}

Edited by PriteshP23: code

0

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.

0

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
0

@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;

Edited by PriteshP23: code

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.