SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    Ajax, Ontario, Canada
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using PHP to Grab an XML Attribute Value

    Here is the XML file in question.
    http://postnzb.com/data.txt

    I need to cycle through each of the "segment" tags and grab the value of the "bytes" attribute, and then add them all together to get the total "bytes" of all of the "segments". I've looked through the PHP simpleXML functions but I can't find anything about grabbing the attribute values.

    How should I go about doing this?

    Thanks for your help.

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    $total=0;
    foreach($xml->xpath('//segment') as $segment){
      $total+=(int)$segment['bytes'];
    }
    Saul

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    Ajax, Ontario, Canada
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help, but I can't get that code to work. It's always returning 0. Am I loading the xml file correctly? Does the xml file have to have the "xml" extension?

    PHP Code:
    $xml simplexml_load_file(ROOT."/files/".$nzbname.".nzb");
    $size_attachment=0;
    foreach(
    $xml->xpath('//segment') as $segment){
        
    $size_attachment+=(int)$segment['bytes'];


  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    Ajax, Ontario, Canada
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried changing the file extension to "xml" - Didn't work.
    I've tried hard coding the path when I load the xml file - Didn't work.
    I've tried echoing a string inside the foreach statement - There's no output.

    The file is obviously not being loaded properly. What am I doing wrong?

    PHP Code:
    define('ROOT'dirname(__FILE__), false);
    $xml simplexml_load_file(ROOT."/files/4e8079fd444d820b93a9677a22d4c621f1126578b56bbad2c04e4833b761d179-2.xml");
    foreach(
    $xml->xpath('//segment') as $segment){
        echo 
    "I'm in a loop!";

    The xml file I'm loading is the one that I linked to in my first post.

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    Ajax, Ontario, Canada
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm also positive that that file exists by that name, because when I try to load a file that doesn't exist, like so:
    PHP Code:
    $xml simplexml_load_file(ROOT."/files/NotHere.xml"); 
    It brings back a "simplexml_load_file" and an "xpath" error that says the file doesn't exist.

    So now I'm thinking it has something to do with the foreach statement you provided me. Any thoughts?

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    Ajax, Ontario, Canada
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $xml simplexml_load_file(ROOT."/files/file.xml");
    print_r($xml); 
    The above outputs the following:
    http://postnzb.com/print.txt

    The foreach statement isn't doing anything.

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    foreach($xml->file as $file){ 
        foreach($file->segments->segment as $segment){
            $size_attachment+=(int)$segment['bytes']; 
        }
    }
    Saul

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    Ajax, Ontario, Canada
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again for your reply
    I just got this working. Would it be more or less efficient then yours?

    PHP Code:
    $xml = new DomDocument;
    $xml->load(ROOT."/files/file.xml");
    $params $xml->getElementsByTagName('segment');

    $size_attachment 0;
    foreach (
    $params as $param) {
           
    $size_attachment+=(int)$param->getAttribute('bytes');


  9. #9
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't know if DOM is any faster or slower than SimpleXML; probably little difference if any. That's fine. Though, I'm not sure why would xpath not work, probably something to do with its implementation in SimpleXML.
    Saul

  10. #10
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    Ajax, Ontario, Canada
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, thanks for your help.
    Much appreciated!

  11. #11
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    Ajax, Ontario, Canada
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just for your information, after a lot of testing I've come to the conclusion that DomDocument is extremely slow with large XML files, say over 1MB. It would timeout after 120 seconds. I'm now using your simpleXML implementation and it works quickly and flawlessly with files over 4mb in size.

    Thanks again.

  12. #12
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Aha, good to know, thanks.
    Saul


Bookmarks

Posting Permissions

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