20i Reseller Hosting

Parsing an XML file

Discussion in 'Scripts and Coding' started by Admin, Dec 15, 2013.

Thread Status:
Not open for further replies.
  1. Admin

    Admin Administrator Staff Member

    Joined:
    Jun 2004
    Posts:
    10,835
    Likes Received:
    314
    If anyone is good with parsing XML files I need some help, the source file contains;

    <![CDATA[
    good data<br/>
    good data too<br/>
    not needed data
    ]]>

    How do I read the first 2 lines into MySql but not the 3rd?

    Many thanks

    Admin
     
  2. Domain Forum

    Acorn Domains Elite Member

    Joined:
    1999
    Messages:
    Many
    Likes Received:
    Lots
    articles.co.uk
     
  3. Jamie101 Netherlands

    Jamie101 Well-Known Member

    Joined:
    Jan 2012
    Posts:
    1,073
    Likes Received:
    16
    Do you know any java? That's what I'd probably use if I were to do that.

    Would offer to give you a hand with it but I've been coding all day and need a break!

    Sent from my GT-I9300 using Tapatalk 2
     
  4. Admin

    Admin Administrator Staff Member

    Joined:
    Jun 2004
    Posts:
    10,835
    Likes Received:
    314
    Not a clue with Java but thanks anyway... and I know the feeling!

    Coffee time ..
     
  5. noonoo1

    noonoo1 Retired Member

    Joined:
    Aug 2011
    Posts:
    376
    Likes Received:
    19
    I might be able to help Admin, pm me.

    Just finished a load of xml parsing on my latest project.
     
  6. noonoo1

    noonoo1 Retired Member

    Joined:
    Aug 2011
    Posts:
    376
    Likes Received:
    19
    Hi Admin,

    PM sent with working code.

    Cheers
     
  7. DaveP United Kingdom

    DaveP Well-Known Member

    Joined:
    Mar 2010
    Posts:
    2,916
    Likes Received:
    52
    If using PHP it would be something like this. :)

    PHP:
    <?php

    $xml_string 
    = <<<XML
    <?xml version="1.0"?>
    <root>
       <item>
           <![CDATA[good data<br/>good data too<br/>not needed data]]>
       </item>
    </root>
    XML;

    $xml simplexml_load_string($xml_string);

    foreach(
    $xml->item as $item) {
       
    // The last parameter is the lines you want + 1 (so for 2 lines it's 3)
       
    $chunks explode('<br/>'$item3);
       
    $good_data = array($chunks[0], $chunks[1]);
       
    print_r($good_data);
    }

    ?>
    Just put your $good_data into your database. :D

    You can test the above here: http://writecodeonline.com/php/
     
  8. noonoo1

    noonoo1 Retired Member

    Joined:
    Aug 2011
    Posts:
    376
    Likes Received:
    19
    The code I pmed over was php.
    Cheers :)

     
  9. DaveP United Kingdom

    DaveP Well-Known Member

    Joined:
    Mar 2010
    Posts:
    2,916
    Likes Received:
    52
    Woops didn't see this at first lol. Hopefully Admin's got it all sorted now anyway. :cool:
     
Thread Status:
Not open for further replies.