Help - Search - Members - Calendar
Full Version: Help with DOM
HTMLHelp Forums > Programming > Client-side Scripting
Kay_Zee
I've got my DOM working and reading/parsing an XML, but in my XML, certain nodes are repeated, eg.:
CODE
<a>
  <b>Something</b>
    <c>Something
      <d>Info</d>
      <d>Info</d>
    </c>
</a>


I have a section of JavaScript set out to do this
CODE
case Node.ELEMENT_NODE: {
            String elementName = currnode.getNodeName();
            if (elementName.equals("b"))
                out.println("<p><u>b: </u>");

            if (elementName.equals("d"))
                out.print("<p>d: ");


With this code, when it reaches certain parts of my XML it'll print:

d: Info
d: Info

But I'd like it to be d: Info, Info. Is that possible?

CODE

case Node.TEXT_NODE: {
            String text = currnode.getNodeValue().trim();
            String textName = currnode.getNodeName();
            if (text.length() > 0) {
                out.println(text);
            }

The above code prints the actual text.
Christian J
QUOTE
With this code, when it reaches certain parts of my XML it'll print:

d: Info
d: Info

That's because each separate D element prints a new P element:

CODE
<p>d:



QUOTE
But I'd like it to be d: Info, Info.

That looks like an array of the D elements' values. I'm not familiar with the kind of javascript you use above, with HTML most people seem to use foo.getElementsByTagName('d') to get an array (AKA "collection") of the D elements in the parent "foo". Then you can loop through that array, and print the child node value of each D element (if you are sure the child node of D is a text node) separated by e.g. commas.

Here's a good DOM reference for HTML: http://www.quirksmode.org/dom/w3c_core.html
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.