XML Import, Is there a script I could customize?
Results 1 to 8 of 8

Thread: XML Import, Is there a script I could customize?

  1. #1
    Member
    Join Date
    Apr 2003
    Location
    Tinley Park, IL
    Posts
    95

    Question XML Import, Is there a script I could customize?

    Sadly, I'm (re)new at coding. I haven't had to do any coding in years and BOY have things updated.

    I need some help with something that doesn't look like it should be as difficult as I'm making it to be.

    I have a pretty simple XML file that I need to pull data from and insert into two related tables.

    Could someone help me? If I could just get a start on the code, I could write it.

    This is the WHOLE file...Of course, the full report would be many more players but here it is.

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?> 
     <Tournament Name="2013-10-15-1930-HOL" TournamentStartDate="2013-10-15 19:38:19" TournamentEndDate="2013-10-15 22:47:53" TournamentComments=""> 
     <Players> 
     <Player FirstName="Joe" LastName="Blow" Rank="35" TournamentPts="20" RoundOut="2" TimeOut="2013-10-15 19:57:51"/> 
     <Player FirstName="Summer" LastName="Sally" Rank="6" TournamentPts="170" RoundOut="10" TimeOut="2013-10-15 22:31:08"/> 
     </players> 
     </Tournament>
    Tournament information needs to be pulled out and inserted into a table called, "myevents" and the player rows need to go to a table called, "event_results"

    ANY HELP on how I could do this would be GREATLY appreciated. Once I get going, I learn fast. I know I'm making this harder than it needs to be.

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,885
    Have you looked at the SimpleXML functions yet? ("Simple" is in the mind of the beholder in this case. ).
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because hes a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  3. #3
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,674
    Quote Originally Posted by NogDog View Post
    Have you looked at the SimpleXML functions yet? ("Simple" is in the mind of the beholder in this case. ).
    +1 for taking a look at that. Otherwise, it's DOMDocument, which is a bit tougher to use, but not impossible, either.

    [EDIT]: I may have been thinking of Jose Solorzano's* simplehtmldom instead, though; not sure if it would apply to this (never tried it on XML), but I've found it to be extremely easy to use and great for some cases of reading HTML docs.

    *Now maintained by someone else, I believe....
    Last edited by dalecosp; 10-18-2013 at 01:47 PM.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  4. #4
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,830
    I think step one would be to find out where you are retrieving your XML from. Are you fetching it from a remote location? Are you reading it from a file? I put it in a file called data.xml and then did this:
    PHP Code:
    <?php
    $xml_string 
    file_get_contents("data.xml");

    $obj = new SimpleXMLElement($xml_string);

    var_dump($obj->Tournament);
    ?>
    I had to change the case of the first P in the closing </Players> tag to get it to parse but then the output indicates that SimpleXML fails to assign any of the Tournament's attributes:
    Code:
    object(SimpleXMLElement)#2 (0) {
    }
    You can get some access to the players like this though:
    PHP Code:
    $xml_string file_get_contents("data.xml");

    $obj = new SimpleXMLElement($xml_string);


    for(
    $i=0$i 5$i++) {
            echo 
    "player " $i ": " $obj->Players->Player[$i]["FirstName"] . "\n";

    I think you might need to tweak your XML document's structure to play better with SimpleXML or use a more sophisticated tool.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  5. #5
    Member
    Join Date
    Apr 2003
    Location
    Tinley Park, IL
    Posts
    95
    Yeah, that "P" was a typo. Eventually, I will be uploading the file and extracting the data. However, I COULD upload the file and store it as method of redundancy.

    I found this code...

    Code:
    <?php
    
    
    if (file_exists('test.xml')) {
        $xml = simplexml_load_file('test.xml');
     
        print_r($xml);
    } else {
        exit('Failed to open test.xml.');
    }
    ?>
    AND it created this output.

    SimpleXMLElement Object ( [@attributes] => Array ( [Name] => 2013-10-15-1930-HOL [TournamentStartDate] => 2013-10-15 19:38:19 [TournamentEndDate] => 2013-10-15 22:47:53 [TournamentComments] => ) [Players] => SimpleXMLElement Object ( [Player] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [FirstName] => Joe [LastName] => Blow [Rank] => 35 [TournamentPts] => 20 [RoundOut] => 2 [TimeOut] => 2013-10-15 19:57:51 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [FirstName] => Summer [LastName] => Sally [Rank] => 6 [TournamentPts] => 170 [RoundOut] => 10 [TimeOut] => 2013-10-15 22:31:08 ) ) ) ) )
    I just don't know what to do with it.

    I know it's giving me some idea of how to write the actual script but I just get more confused every time I read this part of PHP.

    Thanks for all your help so far. I REALLY REALLY appreciate it.
    -Dave

  6. #6
    Member
    Join Date
    Apr 2003
    Location
    Tinley Park, IL
    Posts
    95
    @sneakyimp

    I ran your code and my result was different.
    Code:
    object(SimpleXMLElement)[2]
    Does that change anything?

    And, sorry, but could you explain that FOR statement?
    I don't understand part between ()...What does that represent?

  7. #7
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,830
    Weird that your result was different. Any idea what version of PHP you are running?

    To be honest, I'm not entirely sure how to use the objects returned by these functions. It's sort of trial and error. $obj is going to be some object representing your xml. I would alternately try using var_dump on it and then based on what comes out, I would try object style notation to see if I can get it anything inside it:
    PHP Code:
    var_dump($obj->Players); // is this an array? or another object? 
    If you happen to grab an array, you can use a FOR loop (or FOREACH) to loop through the items you have located.

    The FOR statement I wrote sets the variable $i to 0, executes the code in brackets, and then increments the value of $i by one, and repeats until $i gets to five at which point it stops. I just picked 5 arbitrarily.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  8. #8
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,842
    Quote Originally Posted by dalescop
    +1 for taking a look at that. Otherwise, it's DOMDocument, which is a bit tougher to use, but not impossible, either.
    A distinct advantage of using the DOM is that it's standardised and there's a lot more information about on how to work with it - you're not limited to PHP reosurces.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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