Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-23-2012, 10:05 PM   PM User | #1
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
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
surreal5335 is offline   Reply With Quote
Old 07-24-2012, 03:34 AM   PM User | #2
Alex Vincent
Moderator


 
Join Date: May 2002
Location: Hayward, CA
Posts: 1,428
Thanks: 1
Thanked 19 Times in 17 Posts
Alex Vincent is on a distinguished road
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
Alex Vincent is offline   Reply With Quote
Old 07-24-2012, 04:14 AM   PM User | #3
sean3838
New Coder

 
Join Date: Jan 2012
Posts: 90
Thanks: 1
Thanked 13 Times in 13 Posts
sean3838 is an unknown quantity at this point
Sounds like you should be looping through as its an array of items.
sean3838 is offline   Reply With Quote
Old 07-24-2012, 04:27 AM   PM User | #4
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 906
Thanks: 4
Thanked 212 Times in 211 Posts
tracknut is an unknown quantity at this point
Try replacing:
PHP Code:
$index $cart->count(); 
with:
PHP Code:
$index sizeof ($cart); 
Dave
tracknut is offline   Reply With Quote
Old 07-24-2012, 06:17 AM   PM User | #5
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
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?
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 07-24-2012, 08:23 AM   PM User | #6
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
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
surreal5335 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:13 AM.


Advertisement
Log in to turn off these ads.