JavaScript aligns <XML> from the HTML source - undesired

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lidija

    JavaScript aligns <XML> from the HTML source - undesired

    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.XMLDocu ment;
    if (xmlDom == null) alert("xmlDom == null");
    alert(xmlDom.xm l);

    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

  • Martin Honnen

    #2
    Re: JavaScript aligns &lt;XML&gt; from the HTML source - undesired



    lidija wrote:


    [color=blue]
    > 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.XMLDocu ment;
    > if (xmlDom == null) alert("xmlDom == null");
    > alert(xmlDom.xm l);[/color]

    Frankly what you have above inside of the <XML id="xmltest"> is not even
    well-formed so all you could alert is
    xmltest.XMLDocu ment.parseError .reason and that would tell you that your
    (supposed) XML markup lacks a root element.
    [color=blue]
    > 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>[/color]

    With xmlDom = xmltest.XMLDocu ment; and alert(xmlDom.xm l); 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.

    [color=blue]
    > Is there any other way to read XML with javascript
    > that doesn't do any aligning?[/color]

    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.

    --

    Martin Honnen

    Comment

    • lidija

      #3
      Re: JavaScript aligns &lt;XML&gt; from the HTML source - undesired

      On Wed, 17 Aug 2005 16:07:26 +0200, Martin Honnen wrote:
      [color=blue]
      > lidija wrote:
      >[color=green]
      >> 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.XMLDocu ment;
      >> if (xmlDom == null) alert("xmlDom == null");
      >> alert(xmlDom.xm l);[/color]
      >
      > Frankly what you have above inside of the <XML id="xmltest"> is not even
      > well-formed so all you could alert is
      > xmltest.XMLDocu ment.parseError .reason and that would tell you that your
      > (supposed) XML markup lacks a root element.
      >[/color]

      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="javas cript">
      xmlDOM = xmltest.XMLDocu ment;
      if (xmlDOM == null) alert("xmlDOM == null");
      alert(xmlDOM.xm l);
      </script>
      </BODY>
      </HTML>
      [color=blue][color=green]
      >> 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>[/color]
      >
      > With xmlDom = xmltest.XMLDocu ment; and alert(xmlDom.xm l); 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.[/color]

      Yes, you are correct. It must display the root element (Document). This
      is what I need, it just shouldn't be pretty-printed.
      [color=blue]
      > 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.[/color]

      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
      <Document> contents.
      [color=blue][color=green]
      >> Is there any other way to read XML with javascript that doesn't do any
      >> aligning?[/color]
      >
      > 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.[/color]

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

      Lidija

      Comment

      • Martin Honnen

        #4
        Re: JavaScript aligns &lt;XML&gt; from the HTML source - undesired



        lidija wrote:

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

        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
        preserveWhiteSp ace to true e.g.

        var xmlDocument = new ActiveXObject(' Msxml2.DOMDocum ent.3.0');
        xmlDocument.asy nc = true;
        xmlDocument.pre serveWhiteSpace = true;
        xmlDocument.onr eadystatechange = function () {
        if (xmlDocument.re adyState == 4) {
        if (xmlDocument.pa rseError.errorC ode == 0) {
        alert(xmlDocume nt.xml);
        }
        }
        };
        xmlDocument.loa d('test20050817 01.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.DOMDocum ent.3.0') does not work, I hope you can
        simply use new ActiveXObject(' Microsoft.XMLDO M') 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.


        --

        Martin Honnen

        Comment

        • lidija

          #5
          Re: JavaScript aligns &lt;XML&gt; from the HTML source - undesired

          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.XMLDocu ment.xml;
          str = xmlTabs.replace (/\t/g, "");

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

          Thanks again,
          Lidija

          Comment

          Working...