Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Problem with an xml format.

    Hello,

    I am trying to process an xml file with simplexml_load_file()
    but I notice that the tags that have hyphens in are not working.

    I suspect that it's structure is not valid.

    This is some of it's structure:

    Code:
    <product>
       <id>346</id>
       <name>Covered Call Calculator</name>
       <description>Earn blah blah blah to maximize profits.</description>
       <keywords>option options covered calls stocks </keywords>
       <rank>31</rank>
       <merchant-name>Mathew Merten</merchant-name>
       <merchant-rank>100</merchant-rank>
       <date-added>2007-11-28</date-added>
    </product>
    I use this code to access the tags:
    PHP Code:
    foreach ($xml->xpath('/marketplace/product') as $prod) {

          
    $sql_ins "INSERT INTO my_temp ( seller, seller_rank, title, descrip, keywords )
        VALUES 
        ( '$prod->merchant-name', '$prod->merchant-rank', '$prod_name', $descrip', '$keywords')"

    Now the last 3 tags don't work - I assume that it is because they have hyphens in the name ?


    Do I need to change those hyphens to underscores to make them work ?

    And what would be the best way to make the change ?

    ( Notice that the date also has legitimate hyphens)

    Thanks.



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    I would suggest changing them to underscores. It would work that way. You don't have to worry about the hyphens in the node values. They are fine
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #4
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    The xml file contains approx 2,000 products and is over 2Mb in size.

    Do you suggest that I change use preg_replace to change the entire file
    before processing with simplexml_load_file() ?

    Something like:

    PHP Code:
    $source file_get_contents$file_in  );

    // Locate all tags with hypens

    $pattern '#(\<.+)-( .+\>)#;
    $replacement = '
    $1_$2;

    preg_replace($pattern$replacement$source );

    file_put_contents$file_out$source ); 
    Not sure if my pattern is correct though.



    OR
    (Just read that post )

    Change my code to $link->{'link-code-html'};

    that seems to be the better option.



    BTW - would my reg ex have worked ?



    .
    Last edited by jeddi; 03-05-2010 at 12:30 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #5
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    You could also just use a simple str_replace
    PHP Code:
    $find = array('merchant-name''merchant-rank''date-added');
    $replace = array('merchant_name''merchant_rank''date_added');
    $content str_replace($find$replace$content); 
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #6
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Well,

    I tried changing my script to:

    PHP Code:
    $sql_ins "INSERT INTO my_temp ( seller, seller_rank, title, descrip, keywords )
        VALUES 
        ( '$prod->{'merchant-name'}', '$prod->{'merchant-rank'}', '$prod_name', $descrip', '$keywords')"

    But the MYSQL failed - I guess it is because of the double use of single quotes ?

    Should I use double quotes (excaped)?

    eg:

    PHP Code:
    $sql_ins "INSERT INTO my_temp ( seller, seller_rank, title, descrip, keywords )
        VALUES 
        ( \"$prod->{'merchant-name'}\", \"$prod->{'merchant-rank'}\", '$prod_name', $descrip', '$keywords')"

    Or would that be a problem ?



    .
    Last edited by jeddi; 03-05-2010 at 01:09 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #7
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Use sprintf instead
    PHP Code:
    $sql_ins sprintf("INSERT INTO my_temp ( seller, seller_rank, title, descrip, keywords )
        VALUES 
        ( '%s', '%s', '%s', '%s', '%s')"
    ,$prod->{'merchant-name'}, $prod->{'merchant-rank'}, $prod_name$descrip$keywords) ; 
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

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