SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML into MySQL using PHP

    I am just start with PHP and XML, my xml file (phuket.xml) as

    Code XML:
    <Rx>
    <hotel hno="676386">
    <hno>676386</hno>
    <region>phuket</region>
    <hoteltag>3rdstreet</hoteltag>
    <prov_id>73</prov_id>
    <prov_en>Phuket</prov_en>
    <prov_th>ภูเก็ต</prov_th>
    <hotelname>3 Rd Street Cafe & Guesthouse</hotelname>
    <hotelname_th>สายสามคาเฟ่ แอนด์เกสต์เฮ้าส์</hotelname_th>
    <hoteladr>Kata Night Bazaar 100/3-4, Kata Road, Karon</hoteladr>
    <hotelcity>Muang, Phuket 83100</hotelcity>
    <location>Kata Beach</location>
    <hotellink note="depreciated">
    [url]http://www.R24.org/thailandallhotel.com/phuket/3rdstreet/[/url]
    </hotellink>
    <hotellink2>
    [url]http://thailandallhotel.com.r24.asia/676386/3-Rd-Street-Cafe-and-Guesthouse[/url]
    </hotellink2>
    <minrate_th excl="17%">1020</minrate_th>
    <minrate_us excl="17%">34.22</minrate_us>
    <minrate cur="USD" sym="$" sgn="$" dec="2">34.22</minrate>
    <breakfast>excl</breakfast>
    <promote/>
    <geo_lat>7.8256</geo_lat>
    <geo_long>98.2969</geo_long>
    <distance_mt/>
    <list_ctrl>3</list_ctrl>
    <rating>2.5</rating>
    </hotel>
    <hotel hno="676282">
    <hno>676282</hno>
    <region>phuket</region>
    <hoteltag>woraburiphuket</hoteltag>
    <prov_id>73</prov_id>
    <prov_en>Phuket</prov_en>
    <prov_th>ภูเก็ต</prov_th>
    <hotelname>Woraburi Phuket Resort & Spa</hotelname>
    <hotelname_th>วรบุรี ภูเก็ต รีสอร์ท แอนด์ สปา</hotelname_th>
    <hoteladr>199, 201 Tambol Karon, Amphur Karon,</hoteladr>
    <hotelcity>Phuket 83100</hotelcity>
    <location>Karon Beach</location>
    <hotellink note="depreciated">
    [url]http://www.R24.org/thailandallhotel.com/phuket/woraburiphuket/[/url]
    </hotellink>
    <hotellink2>
    [url]http://thailandallhotel.com.r24.asia/676282/Woraburi-Phuket-Resort-and-Spa[/url]
    </hotellink2>
    <minrate_th excl="17%">1540</minrate_th>
    <minrate_us excl="17%">51.71</minrate_us>
    <minrate cur="USD" sym="$" sgn="$" dec="2">51.71</minrate>
    <breakfast>incl</breakfast>
    <promote>recommend</promote>
    <geo_lat>7.8440</geo_lat>
    <geo_long>98.2952</geo_long>
    <distance_mt/>
    <list_ctrl>3</list_ctrl>
    <rating>3.0</rating>
    </hotel>
    .
    .
    .
    </Rx>


    I try to load above xml file to mysql according to
    http://www.sitepoint.com/forums/show...ySQL-using-PHP

    PHP Code:
    <?php
    $oDOM 
    = new DOMDocument();
    $oDOM->loadXML(file_get_contents('phuket.xml')); #See: [url]http://msdn.microsoft.com/en-us/library/ms762271(VS.85).aspx[/url]
    foreach ($oDOM->getElementsByTagName('hotel') as $oBookNode)
    {
        
    $sSQL sprintf(
            
    "INSERT INTO phuket (hno, region, hoteltag) VALUES ('%s', '%s', '%s')",
            
    mysql_real_escape_string($oBookNode->getElementsByTagName('hno')->item(0)->nodeValue),
            
    mysql_real_escape_string($oBookNode->getElementsByTagName('region')->item(0)->nodeValue),
            
    mysql_real_escape_string($oBookNode->getElementsByTagName('hoteltag')->item(0)->nodeValue)
        );
        
    $rResult mysql_query($sSQL);
        
        if(
    mysql_errno() > 0)
        {
            
    printf(
                
    '<h4 style="color: red;">Query Error:</h4>
                <p>(%s) - %s</p>
                <p>Query: %s</p>
                <hr />'
    ,
                
    mysql_errno(),
                
    mysql_error(),
                
    $sSQL
            
    );
        }    
    }
    ?>
    The result come out with the following error
    Warning: DOMDocument::loadXML() [domdocument.loadxml]: Extra content at the end of the document in Entity, line: 27 in C:\Ampps\www\php\LoadXMLNode.php on line 3

    how can i correct the code.
    Thanks in advance for suggestion.
    Last edited by cpradio; Feb 11, 2013 at 11:56. Reason: Added code tags

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    To answer your question, google seems to suggest that your XML file is mal-formatted.

    Be sure you escape your ampersands (&) using &amp; instead of & in your XML file. Same with quotes ' and " should be escaped too when used in an attribute using &apos; and &quot; respectively.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •