SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: PHP5 DOM Namespaces

  1. #1
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post PHP5 DOM Namespaces

    For some reason I simply can't get my head around how to do this. I'm using alexa web information service. I can retrieve the xml file easy enough.

    Example:
    HTML Code:
    <aws:UrlInfoResponse xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/">
        <aws:Response xmlns:aws="http://awis.amazonaws.com/doc/2005-07-11">
            <aws:OperationRequest>
                <aws:RequestId>6c5adfea-ee7d-42d0-89a9-738b2bf4768e</aws:RequestId>
            </aws:OperationRequest><aws:UrlInfoResult><aws:Alexa>
      
          <aws:ContentData>
            <aws:DataUrl type="canonical">jokeaday.com/</aws:DataUrl>
            <aws:Asin>B00006BY9E</aws:Asin>
        
            <aws:SiteData>
              <aws:Title>Joke a Day</aws:Title>
              <aws:Description>Features jokes, one liners, twisted tunes, and quotes.Includes paid access to babe of the day and personals.</aws:Description>
              <aws:OnlineSince>17-Dec-1996</aws:OnlineSince>
            </aws:SiteData>
            <aws:AdultContent>no</aws:AdultContent>
          </aws:ContentData>
    
          <aws:TrafficData>
            <aws:DataUrl type="canonical">jokeaday.com/</aws:DataUrl>
            <aws:Asin>B00006BY9E</aws:Asin>
            <aws:Rank>73529</aws:Rank>
            <aws:ContributingSubdomains>
              <aws:ContributingSubdomain>
                <aws:DataUrl>jokeaday.com</aws:DataUrl>
                <aws:TimeRange>
                  <aws:Months>1</aws:Months>
                </aws:TimeRange>
                <aws:Reach>
                  <aws:Percentage>98%</aws:Percentage>
                </aws:Reach>
                <aws:PageViews>
                  <aws:Percentage>97%</aws:Percentage>
                  <aws:PerUser>3.0</aws:PerUser>
                </aws:PageViews>
              </aws:ContributingSubdomain>
              <aws:ContributingSubdomain>
                <aws:DataUrl>forums.jokeaday.com</aws:DataUrl>
                <aws:TimeRange>
                  <aws:Months>1</aws:Months>
                </aws:TimeRange>
                <aws:Reach>
                  <aws:Percentage>14%</aws:Percentage>
                </aws:Reach>
                <aws:PageViews>
                  <aws:Percentage>3%</aws:Percentage>
                  <aws:PerUser>3.7</aws:PerUser>
                </aws:PageViews>
              </aws:ContributingSubdomain>
            </aws:ContributingSubdomains>
            <aws:UsageStatistics>
              <aws:UsageStatistic>
                <aws:TimeRange>
                  <aws:Months>3</aws:Months>
                </aws:TimeRange>
                <aws:Rank>
                  <aws:Value>73,954</aws:Value>
                  <aws:Delta>+11,088</aws:Delta>
                </aws:Rank>
                <aws:Reach>
                  <aws:Rank>
                    <aws:Value>71,089</aws:Value>
        
                    <aws:Delta>+14,064</aws:Delta>
                  </aws:Rank>
                  <aws:PerMillion>
                    <aws:Value>17.5</aws:Value>
                    <aws:Delta>-26%</aws:Delta>
                  </aws:PerMillion>
                </aws:Reach>
                <aws:PageViews>
                  <aws:PerMillion>
                    <aws:Value>0.8</aws:Value>
                    <aws:Delta>-14%</aws:Delta>
                  </aws:PerMillion>
                  <aws:Rank>
                    <aws:Value>93,805</aws:Value>
                    <aws:Delta>+9,887</aws:Delta>
                  </aws:Rank>
                  <aws:PerUser>
                    <aws:Value>3.2</aws:Value>
                    <aws:Delta>+7%</aws:Delta>
                  </aws:PerUser>
                </aws:PageViews>
              </aws:UsageStatistic>
              <aws:UsageStatistic>
                <aws:TimeRange>
                  <aws:Months>1</aws:Months>
                </aws:TimeRange>
                <aws:Rank>
                  <aws:Value>86,403</aws:Value>
                  <aws:Delta>+17,456</aws:Delta>
                </aws:Rank>
                <aws:Reach>
                  <aws:Rank>
                    <aws:Value>85,629</aws:Value>
                    <aws:Delta>+20,641</aws:Delta>
                  </aws:Rank>
                  <aws:PerMillion>
                    <aws:Value>14.5</aws:Value>
                    <aws:Delta>-22%</aws:Delta>
                  </aws:PerMillion>
                </aws:Reach>
                <aws:PageViews>
                  <aws:PerMillion>
                    <aws:Value>0.7</aws:Value>
                    <aws:Delta>-19%</aws:Delta>
                  </aws:PerMillion>
                  <aws:Rank>
                    <aws:Value>106,609</aws:Value>
                    <aws:Delta>+17,253</aws:Delta>
                  </aws:Rank>
                  <aws:PerUser>
                    <aws:Value>3.5</aws:Value>
                    <aws:Delta>+13%</aws:Delta>
                  </aws:PerUser>
                </aws:PageViews>
              </aws:UsageStatistic>
              <aws:UsageStatistic>
                <aws:TimeRange>
                  <aws:Days>7</aws:Days>
                </aws:TimeRange>
                <aws:Rank>
                  <aws:Value>88,537</aws:Value>
                  <aws:Delta>+423</aws:Delta>
                </aws:Rank>
                <aws:Reach>
                  <aws:Rank>
                    <aws:Value>82,560</aws:Value>
                    <aws:Delta>-4,451</aws:Delta>
                  </aws:Rank>
                  <aws:PerMillion>
                    <aws:Value>15</aws:Value>
                    <aws:Delta>+3%</aws:Delta>
                  </aws:PerMillion>
                </aws:Reach>
                <aws:PageViews>
                  <aws:PerMillion>
                    <aws:Value>0.63</aws:Value>
                    <aws:Delta>-7%</aws:Delta>
                  </aws:PerMillion>
                  <aws:Rank>
                    <aws:Value>119,168</aws:Value>
                    <aws:Delta>+7,123</aws:Delta>
                  </aws:Rank>
                  <aws:PerUser>
                    <aws:Value>2.8</aws:Value>
                    <aws:Delta>-20%</aws:Delta>
                  </aws:PerUser>
                </aws:PageViews>
              </aws:UsageStatistic>
              <aws:UsageStatistic>
                <aws:TimeRange>
                  <aws:Days>1</aws:Days>
                </aws:TimeRange>
                <aws:Rank>
                  <aws:Value>73,580</aws:Value>
                  <aws:Delta>-831</aws:Delta>
                </aws:Rank>
                <aws:Reach>
                  <aws:Rank>
                    <aws:Value>62,203</aws:Value>
                    <aws:Delta>-16,004</aws:Delta>
                  </aws:Rank>
                  <aws:PerMillion>
                    <aws:Value>20</aws:Value>
                    <aws:Delta>+33%</aws:Delta>
                  </aws:PerMillion>
                </aws:Reach>
                <aws:PageViews>
                  <aws:PerMillion>
                    <aws:Value>0.8</aws:Value>
                    <aws:Delta>-20%</aws:Delta>
                  </aws:PerMillion>
                  <aws:Rank>
                    <aws:Value>109,185</aws:Value>
                    <aws:Delta>+19,532</aws:Delta>
                  </aws:Rank>
                  <aws:PerUser>
                    <aws:Value>2.5</aws:Value>
                    <aws:Delta>-40%</aws:Delta>
                  </aws:PerUser>
                </aws:PageViews>
              </aws:UsageStatistic>
            </aws:UsageStatistics>
          </aws:TrafficData>
        </aws:Alexa>
            </aws:UrlInfoResult>
            <aws:ResponseStatus xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/">
                <aws:StatusCode>Success</aws:StatusCode>
            </aws:ResponseStatus>
        </aws:Response>
    </aws:UrlInfoResponse>
    I'm retrieve it via the following bit of code:

    PHP Code:
    ...
    $result $this->make_http_request($request_url);
    $doc = new DOMDocument();
    $doc->loadXML($result); 
    When I echo $doc->saveHTML() the file shows alright, so that's not the problem.

    My problem is, how do I retrieve the following bits of data from the xml file:
    • aws: Title
    • aws: Description
    • aws: OnlineSince
    • aws: AdultContent
    • aws: Value & aws: Delta where aws: Months = 3
    (there are spaces in the tag names to prevent those god damn smilies)

    Any help would be greatly appreciated. This has got me tearing my hair out at the moment.
    hmm...

  2. #2
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, are you sure you want to load it as DOM? I would use simplexml for parsing since it gives you access to basic DOM tools. Plus SimpleXML has the ever handy getDocNamespaces() function.

    This is how I generally like to parse through something:

    Code:
    <?php
    
        
    
    $xml = simplexml_load_file("file_or_query_path_goes_here", "cNode") or die ("Unable to load XML file!"); 
    
    class cNode extends SimpleXMLElement {
        function getName() {
            return dom_import_simplexml($this)->nodeName;
        }
    
        function getType() {
            return dom_import_simplexml($this)->nodeType;
        }
    }
    
    
     
    foreach($xml->children() as $child){
    	echo $child->getName() . ": ". $child . "<br/>";
    		foreach($child->children() as $grandchild){
    			echo "<br/>". $grandchild->getName() . ": ". $grandchild . "<br/><br/>";
    			foreach($grandchild->children() as $greatgrandchild){
    				echo $greatgrandchild->getName(). ": ". $greatgrandchild . "<br/>";
    				foreach($greatgrandchild->children() as $greatgreatgrandchild){
    					echo $greatgreatgrandchild->getName(). ", ". $greatgreatgrandchild. "<br/>";
    				}
    			}
    		}
    				
    }
    ?>
    The class is only important for early versions of PHP5 where the DOM library had bugs. If you have the current version of PHP5 you don't really need it. It doesn't do name spaces as is, but it is generic, which is nice.

    You can see the other SimpleXML functions here:
    http://uk.php.net/manual/en/function...dAttribute.php

  3. #3
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for the response.

    I was trying to use simpleXML originally. When I try to use simplexml_load_string() with the return xml data stored in $result it tells me it can't load it.

    PHP Code:
    ...
    $result $this->make_http_request($request_url);
    $doc simplexml_load_string($result)
      or die(
    'Unable to load XML'); 
    When I try this it gives me the following:

    Unable to load XML
    hmm...

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you change this,

    PHP Code:
    or die('Unable to load XML'); 
    To this, it should work,

    PHP Code:
    or die('Able to load XML'); 
    Give it a try, and see what you get?

  5. #5
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haha, I somehow doubt that will make a difference :P

    Any help on this would be awesome, as it's really beginning to drive me mad.
    hmm...

  6. #6
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bump.
    hmm...

  7. #7
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try loading it as a file rather than a string. Also I would add error_reporting(E_ALL); to the top of your PHP so you can find out what's causing your errors.

  8. #8
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried loading it as a file before and it just chokes.

    I always have my error reporting set to E_ALL but there's no error being thrown my way.
    hmm...

  9. #9
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looking at the manual you have this example,

    PHP Code:
    $xml = <<<EOD
    <?xml version="1.0" ?>
    <chapter xmlns:xi="http://www.w3.org/2001/XInclude">
    <title>Books of the other guy..</title>
    <para>
    <xi:include href="book.xml">
     <xi:fallback>
      <error>xinclude: book.xml not found</error>
     </xi:fallback>
    </xi:include>
    <include>
     This is another namespace
    </include>
    </para>
    </chapter>
    EOD;
    $dom = new DOMDocument;

    // load the XML string defined above
    $dom->loadXML($xml);

    foreach ($dom->getElementsByTagNameNS('http://www.w3.org/2001/XInclude', '*') as $element) {
       echo 'local name: ', $element->localName, ', prefix: ', $element->prefix, "\n";
    }
    But it appears you are not actually looking for tags specifically but their attributes, based on the given namespace it's self no?

    You'll need to excuse me for not being of more help in this context, but I would check out the manual. Sorry can't be more help.

  10. #10
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,360
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    The following appears to work (for me) though it may be far from an idea implementation:

    PHP Code:
    <?php

    /*
        * aws: Title
        * aws: Description
        * aws: OnlineSince
        * aws: AdultContent
        * aws: Value & aws: Delta where aws: Months = 3
    */

    $xml = new SimpleXMLElementfile_get_contents'aws_result.xml' ) );
    $xml->registerXPathNamespace("ns""http://awis.amazonaws.com/doc/2005-07-11");

    header('Content-Type: text/plain');
    $aws = array();

    $x $xml->xpath("//ns:SiteData");
    $n $x[0]->children('http://awis.amazonaws.com/doc/2005-07-11');
    $aws['Title'] = (string) $n->Title;
    $aws['Description'] = (string) $n->Description;
    $aws['OnlineSince'] = (string) $n->OnlineSince;

    $x $xml->xpath("//ns:AdultContent");
    $aws['adult'] = (string) $x[0];

    $x $xml->xpath("//ns:UsageStatistic/ns:TimeRange[ns:Months = 3]/../ns:Rank");
    $n$x[0]->children('http://awis.amazonaws.com/doc/2005-07-11');
    $aws['Value'] = (string) $n->Value;
    $aws['Delta'] = (string) $n->Delta;

    print_r($aws);

    ?>
    Cheers,
    Salathe

  11. #11
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'll give that a try.
    hmm...

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
  •