JavaScript aligns <XML> from the HTML source - undesired

L

lidija

Hi everybody,

My problem is following: in the HTML source I have an <XML> tag,
containing an XML document. I am trying to read this XML and save it, but
I have problem saving it in the same form as it is in the HTML source.

For example, let's say that the XML tag in my HTML source looks like this:

<XML id="xmltest">
<TAG1>some text here for example</TAG1>
<TAG2>some other text here</TAG2>
<TAG3>
<TAG3a>some third text</TAG3a>
</TAG3>
</XML>

I read the XML and display it:
xmlDom = xmltest.XMLDocument;
if (xmlDom == null) alert("xmlDom == null");
alert(xmlDom.xml);

The alert contains aligned XML, instead of the XML as it was in the
source:
<XML id="xmltest">
<TAG1>some text here for example</TAG1>
<TAG2>some other text here</TAG2>
<TAG3>
<TAG3a>some third text</TAG3a>
</TAG3>
</XML>

When I save (or alert) the XML, it is aligned, instead of the way it was
in the HTML source, what I need.

Since the XML must be saved just the way it is in the HTML source (no
aligning), this isn't good. Did anyone experience this problem already? Is
there any solution? Is there any other way to read XML with javascript
that doesn't do any aligning?

If someone can provide me some help, I would really appreciate it.

TIA, Lidija
 
M

Martin Honnen

lidija wrote:


For example, let's say that the XML tag in my HTML source looks like this:

<XML id="xmltest">
<TAG1>some text here for example</TAG1>
<TAG2>some other text here</TAG2>
<TAG3>
<TAG3a>some third text</TAG3a>
</TAG3>
</XML>

I read the XML and display it:
xmlDom = xmltest.XMLDocument;
if (xmlDom == null) alert("xmlDom == null");
alert(xmlDom.xml);

Frankly what you have above inside of the <XML id="xmltest"> is not even
well-formed so all you could alert is
xmltest.XMLDocument.parseError.reason and that would tell you that your
(supposed) XML markup lacks a root element.
The alert contains aligned XML, instead of the XML as it was in the
source:
<XML id="xmltest">
<TAG1>some text here for example</TAG1>
<TAG2>some other text here</TAG2>
<TAG3>
<TAG3a>some third text</TAG3a>
</TAG3>
</XML>

With xmlDom = xmltest.XMLDocument; and alert(xmlDom.xml); the alert
would never show the <XML id="xmltest"> itself as that is part of the
HTML document and not of the XML so you are not really showing us what
your alert displays.


I suggest you make small test case with well-formed XML inside of an
HTML document that demonstrates the problem and then post a URL that we
can visit telling us which IE version you use.

Is there any other way to read XML with javascript
that doesn't do any aligning?

You do not need an XML data island to load XML, you can also use script
to do that, see
<http://www.faqts.com/knowledge_base/view.phtml/aid/6826/fid/616>
<http://www.faqts.com/knowledge_base/view.phtml/aid/15302/fid/616>

Whether that helps with that "aligment problem" remains to be seen until
you provide some code allowing us to understand what the problem is.
 
L

lidija

Frankly what you have above inside of the <XML id="xmltest"> is not even
well-formed so all you could alert is
xmltest.XMLDocument.parseError.reason and that would tell you that your
(supposed) XML markup lacks a root element.

My mistake, I appologize, you are absolutely right, the root tag was
missing. Following is an example of an HTML file that does what I said:
when it displays the xml, it is aligned.

<HTML>
<BODY>
<XML id="xmltest">
<Document>
<TAG1>some text here for example</TAG1>
<TAG2>some other text here</TAG2>
<TAG3>
<TAG3a>some third text</TAG3a>
</TAG3>
</Document>
</XML>
<script language="javascript">
xmlDOM = xmltest.XMLDocument;
if (xmlDOM == null) alert("xmlDOM == null");
alert(xmlDOM.xml);
</script>
</BODY>
With xmlDom = xmltest.XMLDocument; and alert(xmlDom.xml); the alert
would never show the <XML id="xmltest"> itself as that is part of the
HTML document and not of the XML so you are not really showing us what
your alert displays.

Yes, you are correct. It must display the root element (Document). This
is what I need, it just shouldn't be pretty-printed.
I suggest you make small test case with well-formed XML inside of an
HTML document that demonstrates the problem and then post a URL that we
can visit telling us which IE version you use.

Thanks for the warning, the above code should work on the local machine,
just put it into an HTML file and run it. Alert will display the formatted
You do not need an XML data island to load XML, you can also use script
to do that, see
<http://www.faqts.com/knowledge_base/view.phtml/aid/6826/fid/616>
<http://www.faqts.com/knowledge_base/view.phtml/aid/15302/fid/616>

Whether that helps with that "aligment problem" remains to be seen until
you provide some code allowing us to understand what the problem is.

Thanks for the links, I will look into them. The pretty-printed
XML is obviously result of the DOM parser parsing the XML.

Lidija
 
M

Martin Honnen

lidija wrote:

It must display the root element (Document). This
is what I need, it just shouldn't be pretty-printed.
The pretty-printed
XML is obviously result of the DOM parser parsing the XML.

I can't tell what to change with an XML data island (e.g. the <XML>
element in an HTML document) but with IE 6 using MSXML 3 you can load
the XML from a URL and then before loading you can set
preserveWhiteSpace to true e.g.

var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.3.0');
xmlDocument.async = true;
xmlDocument.preserveWhiteSpace = true;
xmlDocument.onreadystatechange = function () {
if (xmlDocument.readyState == 4) {
if (xmlDocument.parseError.errorCode == 0) {
alert(xmlDocument.xml);
}
}
};
xmlDocument.load('test2005081701.xml');

That way the white space in the XML markup is preserved when
xmlDocument.xml is shown in the alert dialog.

Note that with default IE 5 and IE 5.5 installations new
ActiveXObject('Msxml2.DOMDocument.3.0') does not work, I hope you can
simply use new ActiveXObject('Microsoft.XMLDOM') instead but I have not
tested IE 5 and IE 5.5 in regard to that white space problem, test that
yourself if you need to support IE 5 or 5.5.
 
L

lidija

Thank you for your help.

Since I want a simple solution, I just replaced all occurrences of the
tabs in the string (since XML itself doesn't contain any tabs). I do a
global search for tab characters with regular expression and remove them:

xmlTabs = xmltest.XMLDocument.xml;
str = xmlTabs.replace(/\t/g, "");

For now this suffices: str contains XML as it was before, no pretty-print.

Thanks again,
Lidija
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top