Thread: Parsing XML

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2

    Parsing XML


    Ok so I set up the shopping cart I have for a website to grab a shipping cost based on some variables passed through a url to the shipping company and total charge displayed in an xml statement. So I'm getting the following errors,

    Warning: urlencode() expects parameter 1 to be string, array given in...

    pertaining to this code,

    PHP Code:
    $request_url $base_url '?shipment=<shipment ' urlencode($xml) . '><total ' urlencode($xml2) . '/></shipment>&token=' urlencode($token); 
                        
    $result file_get_contents($request_url); 
    and this error, Fatal error: Call to undefined method DOMNodeList::getAttribute() in...

    pertaining to this code,

    PHP Code:
    $xmlGet = new DOMDocument($result);
                        
    $searchNode $xmlGet->getElementsByTagName('rate');
                        
    $shipping $searchNode->getAttribute('total_charge'); 
    I'm not sure what's going wrong with the first warning but for the second one I think I know why and I'm not sure how to fix it. The XML code looks like this for example,

    Code:
    <rating xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.shippingco.com/XML/rating.xsd">
    <rate weight="20.0" weight_unit="lb" zone="3" transit_time="1" transit_time_guaranteed="false" estimated_delivery_date="2013-07-16" base_charge="23.17" freight_charge="23.17" residential_address_charge="2.75" net_charge="25.92" fuel_surcharge_rate="15.8" fuel_surcharge="3.66" subtotal_charge="29.58" on_hst_charge="3.85" total_charge="33.43"/>
    </rating>
    I'm trying to retrieve the variable inside total_charge however the code I have there I took from the php.net documentation and I didn't see an example of how to take the attribute of the element inside an element. I think the code I have would work if the xml was like this,

    <rate total_charge="33.43" />

    but the xml code is like this,

    <rating>
    <rate total_charge="33.34" />
    </rating>

    So how would I change my code to accomplish that. Thanks for you time and help in advance!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,335
    Rep Power
    594
    1) The first error means one of $xml, $xml2 or $token is not a string. Did you echo each to see what they were?

    2) Get elementsByTagName returns a DOMNodeList. You need to pick which element of the list you want then use 'item' not 'getAttribute'. Use this to see what you have:
    PHP Code:
    $attrs = array();
    for (
    $i 0$i $searchNode->attributes->length; ++$i) {
      
    $node $searchNode->attributes->item($i);
      
    $attrs[$node->nodeName] = $node->nodeValue;
    }
    var_dump($attrs); 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Thanks for the reply. This is what I have for the whole thing, I guess I could of posted it first,

    PHP Code:
    $xml = array(
                            
    'weight_system' => 'IMPERIAL',
                            
    'shipper_number' => '00000005',
                            
    'destination_postal_code' => $data5['zip'],
                            
    'service_type' => '1'
                        
    ); 
                        
    $xml2 = array(
                            
    'total_pieces' => $value,
                            
    'total_weight' => $weight
                        
    );
                        
    $token 'NH5HNNDH'
                        
    $base_url 'https://www.shippingco.com/XML/RatingXML.jsp'

                        
    $request_url $base_url '?shipment=<shipment ' urlencode($xml) . '><total ' urlencode($xml2) . '/></shipment>&token=' urlencode($token); 
                        
    $result file_get_contents($request_url);
                        
                        
    $xmlGet = new DOMDocument($result);
                        
    $searchNode $xmlGet->getElementsByTagName('rate');
                        
    $shipping $searchNode->getAttribute('total_charge'); 
    So I'm wondering how the $xml and $xml2 variables aren't being recognized as strings but obviously theres something going wrong there. Then for the example you put, I'd do something like this,

    PHP Code:
    $xmlGet = new DOMDocument($result);
    $shipping $xmlGet->attributes->items('total_charge'); 
    Although, where would I put the element name for rate in the above code. Thanks for your time and help!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,335
    Rep Power
    594
    They are not recognized as strings because they are arrays.

    If you run the code I gave you, you will see how to reference the values you want.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    ok cool ill try that and post back here.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    So I put i took out the code I had to parse the $results string in put in the code you replied with and echoed out $xml and $xml2 and got these errors, btw I'm just putting in random shipper_number and token for the example.

    ArrayArray
    Warning: urlencode() expects parameter 1 to be string, array given in...

    Notice: Undefined variable: searchNode in...

    Notice: Trying to get property of non-object in...

    and my code now looks like this,

    PHP Code:
    $xml = array(
                            
    'weight_system' => 'IMPERIAL',
                            
    'shipper_number' => '0004440005',
                            
    'destination_postal_code' => $data5['zip'],
                            
    'service_type' => '1'
                        
    ); 
                        
    $xml2 = array(
                            
    'total_pieces' => $value,
                            
    'total_weight' => $weight
                        
    );
                        echo 
    $xml;
                        Echo 
    $xml2;
                        
    $token 'YH4FrrrFFF'
                        
    $base_url 'https://www.shippingco.com/XML/RatingXML.jsp'

                        
    $request_url $base_url '?shipment=<shipment ' urlencode($xml) . '><total ' urlencode($xml2) . '/></shipment>&token=' urlencode($token); 
                        
    $result file_get_contents($request_url);
                        
                        
    $attrs = array(); 
                        for (
    $i 0$i $searchNode->attributes->length; ++$i) { 
                              
    $node $searchNode->attributes->item($i); 
                              
    $attrs[$node->nodeName] = $node->nodeValue
                        } 
                        
    var_dump($attrs); 
    Last edited by andy1212; July 13th, 2013 at 09:57 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    I took out the arrays and did it like this,

    PHP Code:
    $xml =
                    
    'weight_system="IMPERIAL" 
                    shipper_number="0000000" 
                    destination_postal_code="'
    .$data5['zip'].'" 
                    service_type="1"'
    ;
                    
                
    $xml2 =
                    
    'total_pieces="'.$value.'" 
                    total_weight="'
    .$weight.'"'
    So I don't get this error anymore, ArrayArray
    Warning: urlencode() expects parameter 1 to be string, array given in, but when trying to echo out the $xml and $xml2 strings nothing gets echoed.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    ok the above change I made to $xml and $xml2 won't work so I have to put those variables into an array somehow, although I thought I was doing it right, this is the example from php.net for the array(); documentation

    $array = array(
    "foo" => "bar",
    "bar" => "foo",
    );
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    So I var_dumped both arrays and got this,

    array(4) { ["weight_system"]=> string(8) "IMPERIAL" ["shipper_number"]=> string(8) "000000" ["destination_postal_code"]=> string(6) "m1m1m1" ["service_type"]=> string(1) "1" } array(2) { ["total_pieces"]=> string(1) "0" ["total_weight"]=> string(1) "5" }

    so the arrays work fine although I'm not sure what the string(8) and string(1) and array(4) and string(6) means
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,335
    Rep Power
    594
    That means exactly what it says. The content of that element is a string of length 8, etc.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Ok, so im now wondering how would i echo the url to see if it looks ok or the $result once its passed through file_get_contents()? I tried just echoing, echo $request_url; and echo $result; and both echos didnt show anything on the page.

    Also for that code you provided, it throws those other 2 errors with one of them sayin searchNode isnt defined. So if i shouldnt use $searchNode = $xmlGet->getElementsByTagName('rate'); and you said i should use items instead of getElementsByTagName, should i be putting $searchNode = $xmlGet->items('rate');?

    Also should i be using $xmlGet = newDOMDocument($result); or should i just use $result. Sorry just some things that are confusing me.
    Last edited by andy1212; July 14th, 2013 at 09:44 AM.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,335
    Rep Power
    594
    If $request_url does not show anything then that is where your problem starts. You should not expect $result to have anything either in that case. I don't see off hand why the url is not set so you will need to add some debug to follow your logic to see where things go awry.

    $searchNode isn't defined because you removed the previous code that set it. Reread what I wrote. I did not say don't use getElementsByTagName, I said don't use getAttribute.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Ok awesome ill test those out, run some checks and get back to here with my results
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Ok sorry to keep going back and forth with this thread but I don't understand why any of the variables are being echoed out in the url. I have this code right now,

    PHP Code:
    $xml '
                    weight_system="IMPERIAL" shipper_number="000222000" destination_postal_code="'
    .$data5['zip'].'" service_type="1"
                '
    ;
                
                
    $xml2 '
                    total_pieces="'
    .$value.'" total_weight="'.$weight.'"
                '
    ;         
                        
                
    $token 'A1A1WWWWWF'
                
    $base_url 'https://www.shippingco.com/XML/RatingXML.jsp'

                
    $request_url $base_url '?shipment=<shipment ' urlencode($xml) . '><total ' urlencode($xml2) . '/></shipment>&token=' urlencode($token);
                echo 
    $request_url
                
    $result file_get_contents($request_url);
                
                
    $xmlGet = new DOMDocument($result); 
                
    $searchNode $xmlGet->getElementsByTagName('rate');         
                
    $attrs = array(); 
                for (
    $i 0$i $searchNode->attributes->length; ++$i) { 
                      
    $node $searchNode->attributes->item($i); 
                      
    $attrs[$node->nodeName] = $node->nodeValue
                } 
                
    var_dump($attrs); 
    and $request_url echos this,

    https://www.shippingco.com/XML/RatingXML.jsp?shipment=&token=A1A1WWWWWF

    So I don't really know if the variables are being passed or not but I'm thinking they aren't because these 2 errors follow,

    Notice: Undefined property: DOMNodeList::$attributes in...
    Notice: Trying to get property of non-object in...

    Thanks for your time.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,335
    Rep Power
    594
    Try this:
    PHP Code:
    $request_url $base_url '?'.urlencode('shipment=<shipment ' $xml '><total ' $xml2 '/></shipment>&token=' $token); 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo