Create XML file from HTML table

I am attempting to use JQuery to create an XML file from a HTML table.  There will be one or more products ordered.

Here is a link to a JSFiddle that I have been working on, JSFiddle.

I am aware that there are some issues in the HTML, however the code works...for the most part.  My problem is that I can't figure out why it is appending the result of the second row into the first product.

Expected result
<?xml version="1.0" encoding="utf-8"?>
<root>
	<cart>
		<product>
			<number>PM59011</number>
			<date>Thursday, August 13, 2015</date>
			<quantity>1</quantity>
			<id>DP625W</id>
			<cost> $21.45</cost>
			<name>Devon &amp; Jones Stretch Poplin Ladies' Shirt Three-Quarter Sleeve (DP625W)</name>
		</product>
		<product>
			<number>PM59332</number>
			<date>Monday, August 24, 2015</date>
			<quantity>250</quantity>
			<id>Business Card</id>
			<cost> $30.00</cost>
			<name>John Smith</name>
		</product>
	</cart>
</root>

Open in new window


Actual result
<?xml version="1.0" encoding="utf-8"?>
<root>
	<cart>
		<product>
			<number>PM59011</number>
			<date>Thursday, August 13, 2015</date>
			<quantity>1</quantity>
			<id>DP625W</id>
			<cost> $21.45</cost>
			<name>Devon &amp; Jones Stretch Poplin Ladies' Shirt Three-Quarter Sleeve (DP625W)</name>
			<number>PM59332</number>
			<date>Monday, August 24, 2015</date>
			<quantity>250</quantity>
			<id>Business Card</id>
			<cost> $30.00</cost>
			<name>John Smith</name>
		</product>
		<product>
			<number>PM59332</number>
			<date>Monday, August 24, 2015</date>
			<quantity>250</quantity>
			<id>Business Card</id>
			<cost> $30.00</cost>
			<name>John Smith</name>
		</product>
	</cart>
</root>

Open in new window

LVL 12
Jim RiddlesPrepress/OMS SpecialistAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
In this bit of code you select all product nodes when adding the subnodes so they are added to all that exist at that time, a powerful jQuery concept but in this case not wanted:
	$('cart',wsp_xml).append($('<product />', wsp_xml));
	$('product',wsp_xml).append($('<number />', wsp_xml).text(wsp_number));
	$('product',wsp_xml).append($('<date />', wsp_xml).text(wsp_date));
	$('product',wsp_xml).append($('<quantity />', wsp_xml).text(wsp_quantity));
	try {
		wsp_prod_id = wsp_name.match(/\(([^)]+)\)/)[1];
	} catch(e) {
		wsp_prod_id = wsp_product;
	}
    $('product',wsp_xml).append($('<id />', wsp_xml).text(wsp_prod_id));
	$('product',wsp_xml).append($('<cost />', wsp_xml).text(wsp_cost));
	$('product',wsp_xml).append($('<name />', wsp_xml).text(wsp_name));

Open in new window


This can be solved in a number of ways, for example select the last one (not demonstrated) or save the new product node in a variable and use it to add the subnodes like this:
	var prod;
    $('cart',wsp_xml).append(prod = $('<product />', wsp_xml));
	prod.append($('<number />', wsp_xml).text(wsp_number));
	prod.append($('<date />', wsp_xml).text(wsp_date));
	prod.append($('<quantity />', wsp_xml).text(wsp_quantity));
	try {
		wsp_prod_id = wsp_name.match(/\(([^)]+)\)/)[1];
	} catch(e) {
		wsp_prod_id = wsp_product;
	}
    prod.append($('<id />', wsp_xml).text(wsp_prod_id));
	prod.append($('<cost />', wsp_xml).text(wsp_cost));
	prod.append($('<name />', wsp_xml).text(wsp_name));

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim RiddlesPrepress/OMS SpecialistAuthor Commented:
Brilliant!  In retrospect, what was happening was obvious, but not to my tired eyes.  Many thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.