Jquery stripping xml tags
Hello,
I'm trying to make a simple jquery class for use as a Construct 2 plugin. It's for creating an xml inventory. The problem is somewhere in the first function jquery is stripping all the tags from my xml. I've ran alert through the add_item function in various locations, but can't seem to find the problem. Also my remove_item function isn't doing anything.. I think that's because of the tags being stripped in add_item function.
Thanks for taking a look.
All html is here to so anyone can run this in an html file.
- <!DOCTYPE html>
<html>
<head>
<script type="text/JavaScript" src="jquery.js"></script>
</head>
<body>
<script type="text/JavaScript">
// Find out how to use callback inside each.
function inventoryXML()
{
var xml;
this.set_xml = function(xmlSet)
{
this.xml = xmlSet;
}
this.add_item = function(nameSet, typeSet, amountSet, descriptionSet)
{
// Remember to find node where item exists and add to it, if not add it.
var $xml = $('<dummy />').append(this.xml);
// there are no element so add the item
if(this.xml == "<?xml version=\"1.0\" encoding=\"utf-8\"?><inventory><items><\/items><\/inventory>")
{
$xml.find('inventory').children().each(function(){
$xml.find('items').text('<item><name>' + nameSet + '<\/name><type>'+typeSet+'<\/type><amount>' + amountSet + '<\/amount><description>' + descriptionSet + '<\/description><\/item>');
});
}else{
// there are items, check and see if the item exists
$xml.find('items').children().each(function(){
if(parseInt($xml.find('amount').text()) >= 1)
{
// add one to the number of items
$xml.find('amount').text(parseInt($($xml).find('amount').text()) + parseInt(amountSet));
}
// end loop through items children
});
}
this.xml = $xml.text();
$('#test').html("");
$('#test').html(this.xml);
// end of add item function
}
this.remove_item = function(itemSet, amountSet)
{
// find node if exists, if so get value. If value is one remove the node, if value is less subtract 1.
var $xml = $('<dummy />').append(this.xml);
var couldntRemove = false;
$xml.find('items').children().each(function(){
if($xml.find('name').text() === itemSet)
{
// If the amount is greater than the amount to be removed
if(parseInt($xml.find('amount').text()) > parseInt(amountSet))
{
$xml.find('amount').text(parseInt($xml.find('amount').text())- 1);
// if amount is the same remove element
}else if(parseInt($xml.find('amount').text()) == parseInt(amountSet)){
// There is only one left so remove the element.
$($xml).find('item').text("");
// If amount is more remove nothing
}else if(parseInt($xml.find('amount').text()) < parseInt(amountSet)){
// There isn't enough of the item to remove one.
couldntRemove = true;
}
// end of if name = itemSet
}
// end of each
});
this.xml = $xml.text();
$('#test').html("");
$('#test').html(this.xml);
// end remove item function
}
// end of class
}
var thing = new inventoryXML();
thing.set_xml('<?xml version="1.0" encoding="utf-8"?><inventory><items><\/items><\/inventory>');
</script>
<div id="test" style=""></div>
<br/>
<button onclick="thing.add_item('dagger', 'weapon', 2, 'This is a really cool dagger.')">Add Item</button>
<button onclick="thing.remove_item('dagger', 2)">Remove Item</button>
</body>
</html>