Parsing XML and db insertion

Discussion in 'PHP' started by sipherz, Sep 6, 2011.

  1. #1
    Hi

    I have a mysql table with the following: id, name, model, qty

    What I'm looking to do is parse the following XML and depending on the XML, either insert or update rows.

    
    <?xml version="1.0" ?>
    <data>
        <Item>
            <Code>901AF001</Code>
            <ShortDesc>SPECIAL LARYNGOFOAM</ShortDesc>
            <Quantity>0</Quantity>
        </Item>
        <Item>
            <Code>901AF002</Code>
            <ShortDesc>SPECIAL BUCHANAN</ShortDesc>
            <Quantity>0</Quantity>
        </Item>
    </data>
    
    Code (markup):
    Now, if there is a row in the XML with the <Code> of '901AF001' and this record exists in the DB, then the script needs to update the qty field in the DB with the value of <Quantity>

    If there is no record that matches the XML <Code> then this needs to insert in to the DB.

    I've been looking at ways of how to match the XML to the DB row and I'm having alittle bit of a problem.

    Would it it be easier to get each <Code> value, do a search in the DB, if no result is row is returned, then I'll know this record doesn't exist, so insert, if a row is found, simply update?

    I'd like to know other peoples ideas on this, as it's been bothering me for a couple of hours.

    Many thanks
     
    sipherz, Sep 6, 2011 IP
  2. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #2
    mfscripts, Sep 6, 2011 IP
  3. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #3
    shallowink, Sep 6, 2011 IP
  4. sipherz

    sipherz Peon

    Messages:
    7
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok, thanks all. I have a working version, but I am having a problem with some characters that are in the '<ShortDesc>' field.

    Some of the records in the XML are in the form of:

    <ShortDesc>Sticks & Stones</ShortDesc>

    This gives me an error like:

    
    Warning: simplexml_load_file() [function.simplexml-load-file]: stockdata.xml:1: parser error : xmlParseEntityRef: no name in /../import.php on line 5
    
    Warning: simplexml_load_file() [function.simplexml-load-file]: Code><ShortDesc>Sticks & Stones</ShortDesc> in /../import.php on line 5
    
    Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /../import.php on line 5
    
    
    Code (markup):
    It seems not to like & or / chars when parsing. Is there a way around this?

    Thanks
     
    sipherz, Sep 6, 2011 IP
  5. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #5
    A work around for this is to use simplexml_load_string ( use file_get_contents to load the file as a string) and use:

    htmlspecialchars($string, ENT_QUOTES);
     
    shallowink, Sep 6, 2011 IP