-2

Trying to parse some XML and then insert specific parts into various HTML elements.

I'm using the innerHTML routine and whenever I insert <br> tags, formatting as <br /> or <br></br> in the XML document it automatically ignores the / and formats it to <br>

XML source:

&lt;br /&gt;

I have tried various things like replacing the / with the HTML character codes, but still it inserts <br>.

I want <br /> OR <br></br> Why isn't this working?

EDIT:

XML File:

<?xml version="1.0" encoding="iso-8859-1"?>
<questionContainer>

    <title>What is this?</title>

    <body>
    &lt;br /&gt;
    This is the login form to access the KayPop beta. New beta testers are accepted weekly, find our more or... 

    &lt;br / &gt;&lt;br / &gt;
    &lt;a href="#"&gt;Sign up here!&lt;/a&gt;
    </body>


</questionContainer>

Javascript Routine:

function insertXML()
    {
        $.ajax ({
            type: "GET",
            url: 'cd_catalog.xml',
            dataType: "xml",
            success: function( xml )
            {           
                $(xml).find('questionContainer').each(function(){
                    var title = $(this).find('title').text();
                    document.getElementById('q-T').innerHTML = title;

                    var bodyText = $(this).find('body').text();
                    document.getElementById('q-B').innerHTML = bodyText;
                });
            }
        });
    }

Procuded HTML (viewed in Chrome -> Inspect Element):

http://i.imgur.com/0bq3q.png

ツ.jp
  • 315
  • 5
  • 22
  • What is the doctype and what are you using to parse (the innerHTML suggests JavaScript)? – Jason Sperske Jan 07 '13 at 15:05
  • regardless of anything else, an xHTML line break tag should be `
    ` -- `
    ` is not valid; it has to be a self-closing tag.
    – SDC Jan 07 '13 at 15:05
  • 1
    Not to point to the obious, but you are using `innerHTML` - so I would expect to get HTML back - not XML like you do. – hakre Jan 07 '13 at 15:05
  • If you mean that it shows up as `
    ` in the DOM tree, that's perfectly fine. Or where exactly are you seeing the `
    `s and how do you "insert specific parts into various HTML elements"?
    – Felix Kling Jan 07 '13 at 15:05
  • in what context are you working? is this a Javascript program doing this in the browser? What doctype is the HTML using? – SDC Jan 07 '13 at 15:05
  • `<br \/>` maybe? do you need to escape the / in a string? – Zack Jan 07 '13 at 15:06
  • okay, that's the XML document, but what about the doctype of the HTML document you're loading it into? – SDC Jan 07 '13 at 15:06
  • Also please show your code otherwise it's a real guessing club here. – hakre Jan 07 '13 at 15:06
  • 3
    @SDC: `
    ` *is* valid XHTML.
    – BoltClock Jan 07 '13 at 15:07
  • @BoltClock - it's been a long while since I used xhtml in anger, but I was always under the impression that it wasn't. Maybe they always said that just for backward compatibility's sake? I never really thought to check. In any case, I would guess the core issue here is likely because the document isn't using an xhtml doctype anyway. – SDC Jan 07 '13 at 15:09
  • Possible duplicate of: [How do I serialize a DOM to XML text, using JavaScript, in a cross browser way?](http://stackoverflow.com/q/43455/367456) – hakre Jan 07 '13 at 15:10
  • 1
    @SDC — it is valid XHTML but not HTML Compatible (as per Appendix C) XHTML. – Quentin Jan 07 '13 at 15:10
  • @SDC: It's simply because it's XML-based. – BoltClock Jan 07 '13 at 15:11
  • Well I did learn XHTML from w3schools which I now know to be rather unreliable from w3fools, so I should probably reread how to define an XHTML document unless is proper? ALso guys, really appreciate the downvoters if they tell me why they downvoted... I have also edited the post to include all relevant code. – ツ.jp Jan 07 '13 at 15:12
  • I'd strongly recommend not using XHTML. It is almost always far more trouble then it is worth. The [XHTML syntax for HTML 5](http://www.w3.org/TR/html5/the-xhtml-syntax.html#xhtml) is somewhat scary and includes this gem: * This is not strictly a violation of the XML specification, but it does contradict the spirit of the XML specification's requirements.*. For any webpage that isn't being generated by a very-very-xml backend, I'd recommend plain HTML 5 (or HTML 4 Strict). – Quentin Jan 07 '13 at 15:22

1 Answers1

3

The browser keeps a representation of the DOM in memory.

When you read innerHTML it will serialise the DOM to HTML.

There is no way to make a browser serialise it to XHTML, or to control which HTML syntax it will use when there are multiple ways to represent something (i.e. you cannot state a preference for <p id=1> over <p id="1"> or vice versa (ditto <br>, <br />, <br></br>).

If you want XHTML, you will need to walk the nodes in the DOM and render them as XHTML yourself.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Suggested link: [How do I serialize a DOM to XML text, using JavaScript, in a cross browser way?](http://stackoverflow.com/q/43455/367456) – hakre Jan 07 '13 at 15:11