Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    problems getting xml to load

    I have an xml sheet I have created and converted into an object with simpleXml. My problem is that when I do a count() on the <item> tag I always get 1. When I tried to view xml in its own page I get this error

    XML Parsing Error: no element found

    So I ran the xml string through htmlSpecialChars and it produced this on the screen for me.
    Code:
    <?xml version="1.0" standalone="yes"?>
    <cart>
    <item id="0"><product>dvd</product><contract>2133782</contract><price>39.99</price><name>DVD Creator</name></item>
    <item id="1"><product>music</product><contract>2133786</contract><price>29.99</price><name>Music Editor</name></item>
    <item id="1"><product>image</product><contract>2131570</contract><price>4.99</price><name>Image membership</name></item>
    </cart>
    When I do a print_r() on the object, this is what I get:
    Code:
    SimpleXMLElement Object ( [item] => Array (
     [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 0 ) [product] => dvd [contract] => 2133782 [price] => 39.99 [name] => DVD Creator )
     [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 1 ) [product] => music [contract] => 2133786 [price] => 29.99 [name] => Music Editor )
     [2] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 1 ) [product] => image [contract] => 2131570 [price] => 4.99 [name] => Image membership ) 
     )
    The method I am adding items to my object is by:

    PHP Code:
    $items $cart->addChild('item');
    $index $cart->count();
    $items->addAttribute('id'$index);
    $items->addChild('contract''2133782');
    $items->addChild('price''39.99');
    $items->addChild('name''DVD Creator'); 
    The $index always returns 1 as I mentioned before. So it seems that the <item> tag is only getting created once or the rest are not getting recognized.

    What about it am I missing? Would the spaces inbetween the tags cause this problem?
    Last edited by Alex Vincent; 07-24-2012 at 03:32 AM. Reason: adding code tags

  • #2
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts
    I'm not familiar with the addChild method. It's not part of the standard Document Object Model. Have you looked up what it returns?
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    97
    Thanks
    1
    Thanked 13 Times in 13 Posts
    Sounds like you should be looping through as its an array of items.

  • #4
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,343
    Thanks
    11
    Thanked 288 Times in 287 Posts
    Try replacing:
    PHP Code:
    $index $cart->count(); 
    with:
    PHP Code:
    $index sizeof ($cart); 
    Dave

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,393
    Thanks
    13
    Thanked 353 Times in 349 Posts
    Quote Originally Posted by surreal5335 View Post
    The $index always returns 1 as I mentioned before.
    that’s correct according to the description. you create one child element and its count is thus 1.

    Quote Originally Posted by surreal5335 View Post
    So it seems that the <item> tag is only getting created once or the rest are not getting recognized.
    well, you’ve given us the code for the creation of one element. how are we supposed to know whether the error is somewhere else?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks for the replies.

    From what I understand about the count() method for simpleXml its meant to count how of the specified tag exist. Thats why I used
    $items->count();
    After looking at it further though, I noticed count() is meant to count the children of an element... crucial detail I overlooked.
    $cart->count();
    now its working fine.


    As for the items being populated, elements are added to the cart in the same exact manner, the addChild() methods are stored inside a function which has a switch statment to identify which data to plug in, just stored in the same exact series of addChild. Thought it would be redundant to show it all. I gues the other portion of the function would be helpful though.

    This does work for the most part, I am able to iterate through my cart and populate the info on the screen with ease... Its just that it does not seem to be a valid xml object on its own
    Code:
    function addToCart($contract, $cart, $index = 99)
    {
    	echo 'addToCart() found<br />';
    	if($index == 99) // simple append of new <item> to cart
    	{
    		$index = $cart->count();
    		$items = $cart->addChild('item');
    		$items->addAttribute('id', $index);
    	}
    	else
    	{       // inserts data into already existing <item> where $i and $index match
    		$i=0;
    		foreach($cart as $item)
    		{
    			
    			if($i == $index)
    			{
    				$items = $item;
    				break;
    			}
    			$i++;
    		}
    	}
    
    
    		switch($contract)
    		{
    
    			case '2133782':
    				if($index == 99)
    				{
    					$items->addChild('product', 'dvd');
    				}
    				$items->addChild('contract', '2133782');
    				$items->addChild('price', '39.99');
    				$items->addChild('name', 'DVD Creator');
    			break;
                          // and so on and so on
                     }
    }
    Thanks a lot


  •  

    Posting Permissions

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