parsing XHTML with JavaScript?

Discussion in 'Javascript' started by jackwootton@gmail.com, Jun 23, 2007.

  1. Guest

    Hello everyone,

    I understand that XML can be parsed using JavaScript using the XML
    Document object. However, it is possible to parse XHTML using
    JavaScript? I currently listen for DOMMutation events, when the
    events occur I access the node which was inserted or removed
    (event.target). There is only ever about 5 lines of XHTML nested in
    the node, however it would be silly for me to parse it manually using
    methods like hasChildNodes and parentNode if I there is an object that
    could do it for me.

    Many thanks,

    Jack
    , Jun 23, 2007
    #1
    1. Advertising

  2. RobG Guest

    On Jun 24, 8:03 am, "" <>
    wrote:
    > Hello everyone,
    >
    > I understand that XML can be parsed using JavaScript using the XML
    > Document object. However, it is possible to parse XHTML using
    > JavaScript? I currently listen for DOMMutation events, when the
    > events occur I access the node which was inserted or removed
    > (event.target). There is only ever about 5 lines of XHTML nested in
    > the node, however it would be silly for me to parse it manually using
    > methods like hasChildNodes and parentNode if I there is an object that
    > could do it for me.


    You haven't said what it is that you are trying to do - that is, why
    you are parsing the returned XML. The whole point of XML is to
    provide structure, but you want to ignore the structure and parse it
    yourself. So I've got to ask, why are you using XML?

    Perhaps you should be using JSON: <URL: http://www.json.org/ >

    You might find textContent useful, it essentially returns the
    innerHTML with all the tags stripped. Or you might want get the
    actual node that was modified using the event's relatedNode property.
    If you are trying to get attributes of the nodes, you really should
    use DOM methods.

    --
    Rob
    RobG, Jun 24, 2007
    #2
    1. Advertising

  3. Guest

    On Jun 24, 3:25 am, RobG <> wrote:
    > On Jun 24, 8:03 am, "" <>
    > wrote:
    >
    > > Hello everyone,

    >
    > > I understand that XML can be parsed using JavaScript using the XML
    > > Document object. However, it is possible to parse XHTML using
    > > JavaScript? I currently listen for DOMMutation events, when the
    > > events occur I access the node which was inserted or removed
    > > (event.target). There is only ever about 5 lines of XHTML nested in
    > > the node, however it would be silly for me to parse it manually using
    > > methods like hasChildNodes and parentNode if I there is an object that
    > > could do it for me.

    >
    > You haven't said what it is that you are trying to do - that is, why
    > you are parsing the returned XML. The whole point of XML is to
    > provide structure, but you want to ignore the structure and parse it
    > yourself. So I've got to ask, why are you using XML?
    >
    > Perhaps you should be using JSON: <URL:http://www.json.org/>
    >
    > You might find textContent useful, it essentially returns the
    > innerHTML with all the tags stripped. Or you might want get the
    > actual node that was modified using the event's relatedNode property.
    > If you are trying to get attributes of the nodes, you really should
    > use DOM methods.
    >
    > --
    > Rob


    I'm not parsing XML. I'm parsing XHTML.
    , Jun 24, 2007
    #3
  4. wrote:

    > I'm not parsing XML. I'm parsing XHTML.


    XHTML is XML. So an XML parser can deal with it and build an XML DOM, if
    the parser knows XHTML then it can even build an XHTML DOM for the XHTML
    elements in the namespace http://www.w3.org/1999/xhtml. For instance
    with Mozilla you can do

    var xmlDoc = new DOMParser().parseFromString([
    '<html xmlns="http://www.w3.org/1999/xhtml">',
    '<head>',
    '<title>Example</title>',
    '</head>',
    '<body>',
    '<p id="p1">Kibology for all.</p>',
    '</body>',
    '</html>'
    ].join('\r\n'), 'application/xml');

    var ps = xmlDoc.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'p');
    var p = ps[0];
    alert(p + ': ' + p.id);

    and p is an HTMLParagraphpElement having an id property as the parser
    has recognized the namespace of the element and built the XHTML DOM.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jun 24, 2007
    #4
  5. Guest

    On Jun 24, 7:08 am, Martin Honnen <> wrote:
    > wrote:
    > > I'm not parsing XML. I'm parsing XHTML.

    >
    > XHTML is XML. So an XML parser can deal with it and build an XML DOM, if
    > the parser knows XHTML then it can even build an XHTML DOM for the XHTML
    > elements in the namespacehttp://www.w3.org/1999/xhtml. For instance
    > with Mozilla you can do
    >
    > var xmlDoc = new DOMParser().parseFromString([
    > '<html xmlns="http://www.w3.org/1999/xhtml">',
    > '<head>',
    > '<title>Example</title>',
    > '</head>',
    > '<body>',
    > '<p id="p1">Kibology for all.</p>',
    > '</body>',
    > '</html>'
    > ].join('\r\n'), 'application/xml');
    >
    > var ps = xmlDoc.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'p');
    > var p = ps[0];
    > alert(p + ': ' + p.id);
    >
    > and p is an HTMLParagraphpElement having an id property as the parser
    > has recognized the namespace of the element and built the XHTML DOM.
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/


    Thank you. That was my original question (can the XML parser cope
    XHTML). But now, it seems a bit of an obvious question. Thank you
    for your help, I will give it a go.

    Jack
    , Jun 24, 2007
    #5
  6. wrote:

    > That was my original question (can the XML parser cope
    > XHTML). But now, it seems a bit of an obvious question.


    It depends on the browser and its XHTML support. IE for instance uses
    MSXML as its XML parser which is completely separate from the HTML
    parser. Thus if MSXML encounters an element in the XHTML namespace it
    does not do anything special to provide an XHTML DOM element, rather it
    builds an XML DOM element only. So IE with MSXML can parse XHTML but it
    does not build an XHTML DOM. That way you are for instance not able to
    import XHTML element nodes from an MSXML XML DOM document into the HTML DOM.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jun 24, 2007
    #6
  7. On Jun 24, 5:08 am, Martin Honnen <> wrote:
    > For instance
    > with Mozilla you can do
    >
    > var xmlDoc = new DOMParser().parseFromString



    just fyi, opera also supports DOMParser and your example works fine
    here.
    scripts.contact, Jun 24, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    884
  2. chronos3d
    Replies:
    9
    Views:
    776
    Andy Dingley
    Dec 5, 2006
  3. Usha2009
    Replies:
    0
    Views:
    1,133
    Usha2009
    Dec 20, 2009
  4. xhtml champs
    Replies:
    0
    Views:
    527
    xhtml champs
    Aug 1, 2011
  5. Replies:
    14
    Views:
    203
    David Golightly
    Sep 22, 2007
Loading...

Share This Page