Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

XML Read by XPath DOM

General discussions related to php

Moderators: macek, egami, gesf

XML Read by XPath DOM

Postby Pritesh23 » Tue Aug 13, 2013 2:08 am

I would like to get Type, Id and Height values in database table. It is not working. There is no error message. :help:

XML File:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<Physical>
<Catalog>
<EquipmentSpec>
<Reference>030_17</Reference>
<Type>Duplexeur</Type>
<Characteristic><CharacteristicName>Tilt</CharacteristicName>
<CharacteristicValue>0</CharacteristicValue></Characteristic>
</EquipmentSpec>
<EquipmentSpec>
<Reference>030_19</Reference>
<Type>Node</Type>
<Characteristic><CharacteristicName>Tilt</CharacteristicName>
<CharacteristicValue>0</CharacteristicValue></Characteristic>
</EquipmentSpec>
</Catalog>
<Installed>
<Equipment>
<Id>26</Id><Reference>MAT_4</Reference>
<Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>160</CharacteristicValue></Characteristic>
</Equipment>
<Equipment>
<Id>27</Id><Reference>MAT_6890</Reference>
<Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>180</CharacteristicValue></Characteristic>
</Equipment>
</Installed>
</Physical>


CODE:

Code: Select all
if (file_exists("test.xml"))
{
    $xml = simplexml_load_file("test.xml");
}
else
{
    echo "error file";
}
$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->validateOnParse = true;
$dom->strictErrorChecking = FALSE ;
@$dom->load($xml);

$xpath = new DOMXpath($dom);
$mtest = fopen("mtest", "w+");

foreach ($xml->Catalog->xpath("EquipmentSpec") as $n => $equipmentspec)
{ // xpath for the numeric key
    $equipment = $xml->Installed->Equipment[$n];

    $Type = (string)$equipmentspec->Type;
    $Id = (int)$equipment->Id;
    $Height = (int)current($equipment->xpath("Characteristic[CharacteristicName='Height']/CharacteristicValue")); // a search
   
    echo $Type;
    echo $Id;
    echo $Height;
   
    $sql = "INSERT INTO machines (`Type`, `Id`, `Height`) VALUES ('{$Type}', {$Id}, {$Height})";
    $req = new requete($site->db, $sql);
    echo $sql;
}
 fclose($mtest);


THANKS IN ADVANCED FOR YOUR INPUT. :)
Pritesh23
New php-forum User
New php-forum User
 
Posts: 25
Joined: Wed Jul 17, 2013 12:42 pm

Re: XML Read by XPath DOM

Postby johnj » Tue Aug 13, 2013 2:22 am

johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: XML Read by XPath DOM

Postby Pritesh23 » Tue Aug 13, 2013 2:55 am

Well, i have already seen this article. Thanks.
Pritesh23
New php-forum User
New php-forum User
 
Posts: 25
Joined: Wed Jul 17, 2013 12:42 pm

Re: XML Read by XPath DOM

Postby johnj » Tue Aug 13, 2013 4:02 am

that means you did not see this article properly.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: XML Read by XPath DOM

Postby Pritesh23 » Wed Aug 14, 2013 2:28 am

Well, I have 850 Mb XML file and i just would like to present small part of it once again:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<Physical>
    <Catalog>
        <EquipmentSpec>
            <Reference>MAT_4</Reference>
            <Type>Duplexeur</Type>
            <Characteristic><CharacteristicName>Tilt</CharacteristicName>
            <CharacteristicValue>0</CharacteristicValue></Characteristic>
        </EquipmentSpec>
        <EquipmentSpec>
            <Reference>MAT_6890</Reference>
            <Type>Node</Type>
            <Characteristic><CharacteristicName>Tilt</CharacteristicName>
            <CharacteristicValue>2</CharacteristicValue></Characteristic>
        </EquipmentSpec>
        <EquipmentSpec>
            <Reference>MAT_50</Reference>
            <Type>Single</Type>
            <Characteristic><CharacteristicName>Tilt</CharacteristicName>
            <CharacteristicValue>5</CharacteristicValue></Characteristic>
        </EquipmentSpec>
    </Catalog>
    <Installed>
        <Equipment>
            <Id>26</Id><Reference>MAT_4</Reference>
            <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>160</CharacteristicValue></Characteristic>
        </Equipment>
        <Equipment>
            <Id>27</Id><Reference>MAT_6890</Reference>
            <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>140</CharacteristicValue></Characteristic>
        </Equipment>
        <Equipment>
            <Id>28</Id><Reference>MAT_50</Reference>
            <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>180</CharacteristicValue></Characteristic>
        </Equipment>
    </Installed>
</Physical>


EXPECTED output:

Code: Select all
echo $Type;
  $sql = "INSERT INTO LTE_noria_antennes (`Type`,`Id`,`Height`) VALUES ('{$Type}', '{$Id}', '{$Height}')";
  $req = new requete($site->db, $sql);


It should insert in database table like this:


RefId** Type Id Height
1 Duplexeur 26 160
2 Node 27 140
3 Single 28 180

// **RefId is column auto_incriment to count rows.



Thanks in advanced. :help:
Pritesh23
New php-forum User
New php-forum User
 
Posts: 25
Joined: Wed Jul 17, 2013 12:42 pm

Re: XML Read by XPath DOM

Postby johnj » Wed Aug 14, 2013 5:31 am

Code: Select all
$dom = new DomDocument();
$dom->load("test_xml.xml"); // your xml file

$xpath  = new DOMXPath($dom);
$result   = $xpath->query("/Physical/Catalog/EquipmentSpec/Type");

echo '<br/><br/>';
foreach ($result as $element)
{
    echo "<br/> Type = ".$element->nodeValue."\n";
}

$result   = $xpath->query("/Physical/Installed/Equipment/Id");

echo '<br/><br/>';
foreach ($result as $element)
{
    echo "<br/>Id = ".$element->nodeValue."\n";
}

johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: XML Read by XPath DOM

Postby Pritesh23 » Wed Aug 14, 2013 6:14 am

Thanks. :) But It is giving result: :?

Code: Select all
<br/><br/><br/><br/>


I think We need to correct this:

Code: Select all
echo "<br/> Type = ".$element->nodeValue."\n";


:help:
Pritesh23
New php-forum User
New php-forum User
 
Posts: 25
Joined: Wed Jul 17, 2013 12:42 pm

Re: XML Read by XPath DOM

Postby johnj » Wed Aug 14, 2013 6:47 am

It is already there as echo "<br/> Type = ".$element->nodeValue."\n";
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: XML Read by XPath DOM

Postby johnj » Wed Aug 14, 2013 6:48 am

I am getting the right Id and Type as output. If you are not getting it then you are doing something wrong somewhere.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: XML Read by XPath DOM

Postby Pritesh23 » Wed Aug 14, 2013 7:29 am

Can you tell me OUTPUT for this xml file ??

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<Physical>
    <Catalog>
        <EquipmentSpec>
            <Reference>MAT_478788</Reference>
            <Type>Duplexeur</Type>
            <Characteristic><CharacteristicName>Tilt</CharacteristicName>
            <CharacteristicValue>0</CharacteristicValue></Characteristic>
        </EquipmentSpec>
        <EquipmentSpec>
            <Reference>MAT_6890</Reference>
            <Type>Node</Type>
            <Characteristic><CharacteristicName>Tilt</CharacteristicName>
            <CharacteristicValue>2</CharacteristicValue></Characteristic>
        </EquipmentSpec>
        <EquipmentSpec>
            <Reference>MAT_50</Reference>
            <Type>Single</Type>
            <Characteristic><CharacteristicName>Tilt</CharacteristicName>
            <CharacteristicValue>5</CharacteristicValue></Characteristic>
        </EquipmentSpec>
    </Catalog>
    <Installed>
        <Equipment>
            <Id>26</Id><Reference>MAT_4</Reference>
            <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>160</CharacteristicValue></Characteristic>
        </Equipment>
        <Equipment>
            <Id>27</Id><Reference>MAT_6890</Reference>
            <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>140</CharacteristicValue></Characteristic>
        </Equipment>
        <Equipment>
            <Id>28</Id><Reference>MAT_50</Reference>
            <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>180</CharacteristicValue></Characteristic>
        </Equipment>
    </Installed>
</Physical>


Awaiting your response,
Pritesh23
New php-forum User
New php-forum User
 
Posts: 25
Joined: Wed Jul 17, 2013 12:42 pm

Re: XML Read by XPath DOM

Postby johnj » Thu Aug 15, 2013 8:46 am

Why don't you try?. I showed you an example and also gave a link to the article that teaches you how to do it.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: XML Read by XPath DOM

Postby Pritesh23 » Fri Aug 16, 2013 1:54 am

I did. It is not inserting in table and it should insert TWO rows as TWO Type are matching. :)

XML
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<Physical>
   <Catalog>
      <EquipmentSpec>
         <Reference>MAT_48985</Reference>
         <Type>Duplexeur</Type>
         <Characteristic><CharacteristicName>Tilt</CharacteristicName>
         <CharacteristicValue>0</CharacteristicValue></Characteristic>
      </EquipmentSpec>
      <EquipmentSpec>
         <Reference>MAT_6890</Reference>
         <Type>Node</Type>
         <Characteristic><CharacteristicName>Tilt</CharacteristicName>
         <CharacteristicValue>2</CharacteristicValue></Characteristic>
      </EquipmentSpec>
      <EquipmentSpec>
         <Reference>MAT_50</Reference>
         <Type>Single</Type>
         <Characteristic><CharacteristicName>Tilt</CharacteristicName>
         <CharacteristicValue>5</CharacteristicValue></Characteristic>
      </EquipmentSpec>
   </Catalog>
   <Installed>
      <Equipment>
         <Id>26</Id><Reference>MAT_4</Reference>
         <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>160</CharacteristicValue></Characteristic>
      </Equipment>
      <Equipment>
         <Id>27</Id><Reference>MAT_6890</Reference>
         <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>140</CharacteristicValue></Characteristic>
      </Equipment>
      <Equipment>
         <Id>28</Id><Reference>MAT_50</Reference>
         <Characteristic><CharacteristicName>Height</CharacteristicName><CharacteristicValue>180</CharacteristicValue></Characteristic>
      </Equipment>
   </Installed>
</Physical>



CODE:
Code: Select all
foreach ($xml->xpath('Catalog') as $spec)
{
    foreach ($spec->xpath('//EquipmentSpec') as $sub)
    {
        $Type = $sub->Type;
      echo $Type;
    }
    foreach ($spec->xpath('//Equipment') as $eq)
    {
        $Id = $eq->Id;
      echo $Id;
        foreach ($eq->Characteristic as $c)
        {
            if ($c->CharacteristicName == 'Azimut')
            {
                $Azimut = $c->CharacteristicValue;
            }
        }   
    }

    $sql = "INSERT INTO machines (`Type`,`Id`,`Azimut`) VALUES ('".$Type."','".$Id."','".$Height."')";
    $req = new requete($site->db, $sql);
    echo $sql;
   if (!$req)
   {
      $message  = 'Invalid query: ' . mysql_error() . "\n";
      $message .= 'Whole query: ' . $sql;
      die($message);
   }
}


OUTPUT

Code: Select all
DuplexeurNodeSingle262728INSERT INTO LTE_noria_antennes (`Type`,`Id`,`Azimut`) VALUES ('Single','28','180')



I know there is little mistake but i couldn't solve it out. Thanks for input. :help:
Pritesh23
New php-forum User
New php-forum User
 
Posts: 25
Joined: Wed Jul 17, 2013 12:42 pm

Re: XML Read by XPath DOM

Postby johnj » Fri Aug 16, 2013 9:40 pm

if it is not inserting write error trap routine/statements and trap the error.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: XML Read by XPath DOM

Postby Pritesh23 » Mon Aug 19, 2013 1:41 am

I created "machines_type" table to store all Type Values. The main table is "machines".

I need to solve Update Query with LEFT JOIN :help:

Code: Select all
 UPDATE machines SET machines.Type = machines_type.Type
   WHERE Reference IN (SELECT machines.Reference, machines_type.Reference
   FROM machines
   LEFT JOIN machines_type
   ON machines.Reference = machines_type.Reference
   WHERE machines.Id IS NOT NULL )


Error:
#1241 - Operand should contain 1 column(s)



:help: :help:
Pritesh23
New php-forum User
New php-forum User
 
Posts: 25
Joined: Wed Jul 17, 2013 12:42 pm

Re: XML Read by XPath DOM

Postby Pritesh23 » Mon Aug 19, 2013 6:21 am

Finally, its SOLVED.....!!!! :D
Code: Select all
UPDATE machines LEFT JOIN machines_type ON
machines.Reference = machines_type.Reference
SET machines.Type = machines_type.Type
WHERE
machines_type.Reference IS NOT NULL


:offtopic:
Pritesh23
New php-forum User
New php-forum User
 
Posts: 25
Joined: Wed Jul 17, 2013 12:42 pm


Return to PHP General

Who is online

Users browsing this forum: TurnitinBot [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.