Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I write XML strings in Javascript?

Status
Not open for further replies.

meadensitz

Programmer
Feb 14, 2008
2
US
<script type="text/javascript" >

function testMe() {

var xmld = "<account type='asset' id='create' name='modified expeses' counterWeight='debit' />";

var xmld1 = encodeURI(xmld);
var xmld2 = encodeURIComponent(xmld);
var xmld3 = escape(xmld);
abc = "<abc />";
abc1 = '<table border="0" width="100%" align="center" />';
var abc2 = "abc";
var abc3 = "attr='
alert(xmld1);
alert(xmld2);
alert(xmld3);
alert(abc);
alert(abc1);
alert(abc2);
alert(abc3);

}

</script>
...

I can't get a block of Javascipt to printout a string of XML. I have assume that the browser is treating it as a tag

inside of the html document - When I try to create a variable 'var abc2 = "<abc";', the browser (FF in this case) throws

a parsing error:

XML Parsing Error: not well-formed
Location: file:///C:/Projects/bookkeeping/webkell/webkell.xhtml
Line Number 117, Column 20: var abcz = "<abc";
----------------------------------------^


Does anyone know how I can manipulate XML strings in a browser <script type="text/javascript"> block?

Thanks in advance.
 
Put your javascript into an external .js file.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Webflo
 
>I can't get a block of Javascipt to printout a string of XML.
printout where?
>inside of the html document
what is not inside?

If you process xhtml as xml, your script section should be made cdata.
[tt]
<script type="text/javascript">
<![CDATA[
block?
]]>
</script>[/tt]
 
Ok, the CDATA suggestion worked. And I haven't tried the external .js suggestion yet. This is going to be a larger application, so I'll have to externalize my .js files in the future. Thanks for the feedback.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top