Few Questions about org.w3c.dom.Element

Discussion in 'Java' started by RC, Apr 10, 2008.

  1. RC

    RC Guest

    In that org.w3c.dom.Element interface

    I can getTagName()
    But
    1) How do I do know is it an empty tag or not?

    <tag ..... />

    2) How do I know when will be a close tag?

    <tag1>
    <tag2>xxxx</tag2>
    <tag3 ..... />
    <tag4 ..... />xxxxx</tag4>
    .......
    </tag1>

    Thank Q very much in advance!
     
    RC, Apr 10, 2008
    #1
    1. Advertising

  2. "RC" <> wrote in message
    news:ftm02s$ln2$...
    > In that org.w3c.dom.Element interface
    >
    > I can getTagName()
    > But
    > 1) How do I do know is it an empty tag or not?
    >
    > <tag ..... />
    >
    > 2) How do I know when will be a close tag?
    >
    > <tag1>
    > <tag2>xxxx</tag2>
    > <tag3 ..... />
    > <tag4 ..... />xxxxx</tag4>
    > ......
    > </tag1>
    >
    > Thank Q very much in advance!


    org.w3c.dom.Element doesn't represent the start tag; it represents the
    entire element. Anything occuring between the start and end tag will beomce
    children of that element. E.g.

    <start>This is <i>italicized</i>text.</start>

    resluts in five Nodes:

    1. An Element with the tag name "start"
    2. A Text with the node value "This is "
    3. An Element with tag name "i"
    4. A Text with the node value "italicized"
    5. A Text with the node value "text."

    2, 3, and 5 are (in that order) the child node of 1. 4 is the child of 3.

    And in DOM, there's no way to distinugish between, say,

    <start></start>

    and

    <start/>

    In both case, you see an Element wth no children.
     
    Mike Schilling, Apr 10, 2008
    #2
    1. Advertising

  3. RC wrote:
    > In that org.w3c.dom.Element interface


    Ah, so this is a question on the DOM.

    > I can getTagName()
    > But
    > 1) How do I do know is it an empty tag or not?
    >
    > <tag ..... />


    <tag></tag> and <tag /> are the same thing as far as the DOM is closed.
    That is the job of the parser, and is generally not important. However,
    one can detect a state equivalent to said tag by checking for any
    children of node type TEXT_NODE.

    > 2) How do I know when will be a close tag?
    >
    > <tag1>
    > <tag2>xxxx</tag2>
    > <tag3 ..... />
    > <tag4 ..... />xxxxx</tag4>
    > ......
    > </tag1>


    You seem to be parsing XML. The DOM is wholly an in-memory
    representation, whereas XML is a text format representation of the same
    tree. Therefore no close tags exist in the DOM. The example you give
    will be noted in the DOM as such:

    Element tag1
    |
    +-- Element tag2
    | |
    | +-- Text node "xxxx"
    |
    +-- Element tag3
    | |
    | +-- Attribute attr1 value val1
    |
    +-- Element tag4
    | |
    | +-- Attribute attr1 value val1
    | |
    | +-- Text node "xxxxx"
    |
    < other elements, text nodes, etc. >

    No close tags are needed nor involved.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Apr 10, 2008
    #3
  4. > 1) How do I do know is it an empty tag or not?
    > <tag ..... />


    XML doesn't distinguish between <tag></tag> and <tag/>. Nor does the
    DOM. If you want to know whether it has content, check whether it has
    children.

    > 2) How do I know when will be a close tag?


    Don't think in terms of tags. Think in terms of elements. An element is
    made up of an open tag and its close tag, or a self-closing tag -- as I
    just said, they're the same thing. Everything inside it is its children.
    There is no separate representation of the close tag.
     
    Joseph J. Kesselman, Apr 10, 2008
    #4
    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. Wendy S
    Replies:
    1
    Views:
    6,366
    Darren Davison
    Aug 5, 2003
  2. Praveen Chhangani

    Converting a org.jdom DOC to org.w3c DOC

    Praveen Chhangani, Aug 5, 2003, in forum: XML
    Replies:
    2
    Views:
    971
    Johannes Koch
    Aug 7, 2003
  3. RC
    Replies:
    3
    Views:
    554
    Joseph J. Kesselman
    Apr 10, 2008
  4. Alan
    Replies:
    6
    Views:
    1,607
  5. Adam Funk
    Replies:
    11
    Views:
    6,115
    Adam Funk
    Jan 21, 2010
Loading...

Share This Page