user1404191
user1404191

Reputation: 21

DOM object of XML, to HTML

I have a XML code with a XML table

<displayTable>
<tr>
<td> items </td>
<..>
</displayTable>

its structured just exactly like a table in HTML but the name of the element is different. I then parseXML it with jQuery, and get a DOM object. But now when I'm trying to get the html elements of the tables, but i don't get the HTML table(no errors, I get the items within the tags) when using .text() and .html()... anyone know why?

EDIT: Sorry for the confusion. So its similar to this js. I want the HTML code from the XML. So just the A... At the line&&& it produces "Uncaught TypeError: Cannot call method 'replace' of undefined" error var str = "A";

$('#code-analysis').html(str);
console.log(str);

// parse string of XML code
var xmlString = str;
var xml = $.parseXML( xmlString );
$xml = $( xml );


// print to table in console
var displayTable = $xml.find( 'displayTable' ).children;
console.log ( displayTable );


// print table.text() to console, 
// but not in the HTML format 
// just the text within the element
var displayTableText = displayTable.text();
console.log ( displayTableText );              // A


// print table.html() to console
var displayTableHTML = displayTable.html();  // &&&
console.log ( displayTableHTML );               

Upvotes: 2

Views: 712

Answers (1)

bfavaretto
bfavaretto

Reputation: 71908

You have to use .children(), not .children. It's a function call in jQuery, not a property. Also, you can pass the children to .html() without having to convert them to string (I was getting an error trying to do that).

With that, it's just a matter of adding the table rows to a <table> element: http://jsfiddle.net/cRRAM/3/

Upvotes: 2

Related Questions