41 users online (0 members and 41 guests)  


  Results 1 to 6 of 6

Related

  1. read files in multiple directories?    Forum: CGI Perl Forum
    Replies: 2
  2. parsing string    Forum: CGI Perl Forum
    Replies: 1
  3. Replies: 2
  4. Help parsing html for URLs    Forum: CGI Perl Forum
    Replies: 0
  5. Replies: 5
  1. #1
    StangGT5093's Avatar
    New User

    Status
    Offline
    Join Date
    Nov 2008
    Posts
    4

    Help parsing multiple XML files in PHP 5

    Hello, I'm making my first attempt at parsing xml files. I've really only worked with basic php developing a wordpress theme, so I figured doing it in php would be best.

    I read about the SimpleXML extension in PHP 5, which I'm going to use if possible for what I'm trying to do, which is essentially recreate a table of betting odds.

    Here's an example of what I'm trying to recreate (sorry the board wouldn't let me post a link): www.bestfightodds.com.

    I don't care about the bells and whistles, just the table, although if I could figure out the best way to highlight the best line for each row, that would be awesome.

    Basically, each column is coming from a different XML file. I can figure out how to parse the name and matching odds for one file, but I'm not sure how it would be done with multiple files. Specifically, matching up the correct odds to the correct name for the files of which I'm not parsing the names. I'm not sure how to do that.

    Also, Is it even possible for it all to run automatically or would I need to be constantly changing the code in the parser every time a new event comes up?

    Any help would be much appreciated, thanks!

  2. #2
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: Help parsing multiple XML files in PHP 5

    I will attempt to answer all your questions.

    Parsing multiple files:

    This is done the same way as with a single file. The difference is that each time you open a file, you will need to assign it to a new handle.
    PHP Code:
    $xml1 = new SimpleXMLElement('http://example.org/document1.xml'NULLTRUE);
    $xml2 = new SimpleXMLElement('http://example.org/document2.xml'NULLTRUE);
    $xml3 = new SimpleXMLElement('http://example.org/document3.xml'NULLTRUE);
    $xml4 = new SimpleXMLElement('http://test.org/document.xml'NULLTRUE); 
    You can then perform operations on each instance of the handle. They are individual, and not related to each other.


    Highlight Best Line:

    This is a simple if...then statement. You need to figure out what makes the line the best choice, and go from there.



    Matching Up Columns from various XML files:

    This should not be as big a deal as you think. Especially if they come from the same site. If they come from different sites, the XML files will probably have different structures, and that is where there could be issues.

    I am going to post a basic XML tutorial in the next couple days that should help you understand more about what is going on and how things can be related.



    Running the parser automatically:

    This will not be an issue for you unless the structure (not data) of one of the XML files your are pulling gets changed.

    That is the beauty of how RSS feeds work on news sites. They set up the reader to read the XML file from the news site. As long as the structure of the XML file does not change, they don't ever have to touch the code again.



    I hope that was of some help. I will be glad to help more as needed.

  3. #3
    StangGT5093's Avatar
    New User

    Status
    Offline
    Join Date
    Nov 2008
    Posts
    4

    Re: Help parsing multiple XML files in PHP 5

    Thank you very much for your response and your help!

    Any kind of demo/tutorial would be great!

    To answer your question about whether the feeds come from different sites, they do. I have two of the sites feed addresses already and they do have different structures. I was reading about XSLT and how you can use that to merge multiple feeds together. Is that what I would need to do?

    Thanks again for all your help!

  4. #4
    StangGT5093's Avatar
    New User

    Status
    Offline
    Join Date
    Nov 2008
    Posts
    4

    Re: Help parsing multiple XML files in PHP 5

    There was two other things that I forgot to mention that I'm afraid may complicate it even further.

    Not all of the feeds include all the same names and cooresponding odds. For example, XML feeds 1, 3, 4, 6 may have 20 names at one time, while feeds 2, 5 have 10 names, and 7 & 8 have 12 names. In such cases, I would need to put a "-" or "N/A" in the table if the feed doesn't have the data for a certain name.

    Plus not every feed includes the event info that the names is tied to. For instance, XML feed 1 may include names A, B, C, D which are tied to event Z, but XML feed 2 only includes names A, B, C, D but no mention of event Z at all.

    This is why I'm thinking I need to somehow merge all the feeds into one and output it from that. I've never done this though, so I could definitely be wrong.

  5. #5
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: Help parsing multiple XML files in PHP 5

    I am looking into PHP, XPath, and XSLT more so that I know more of what they are capable of. Personally, I would go through each XML file and load into a multidimensional associative array with the name as the primary key. However, this may be more processor intensive than you are looking for. I will look more on this today and get back to you either later today or tomorrow.

  6. #6
    StangGT5093's Avatar
    New User

    Status
    Offline
    Join Date
    Nov 2008
    Posts
    4

    Re: Help parsing multiple XML files in PHP 5

    Thanks jthayne!

    I don't know if this is the way to go, but I was reading about XSLT and how you can transform the structures. Since all the XML feeds are different structures, how about individually transforming them all into one standard structure, and then parsing them from there. The only thing with that though is I know at least one of the XML files doesn't contain the event name data.



Tags for this Thread