0

I want to get the Element-Node Sourrounding the Text-Node. According to this Element contains a Text-Node, vice versa a text-node should be surounded by a element-node.

I need the Element-node(openingTime) to get an attribute (day), but the NodeList (getChildNodes() called from the node openingHours) only gives me Text-Nodes with their parents being a Element-node (openingHours), but not the one surrounding the text-Node, but instead the one above. If anyone asks, i need to use DOM, because, as I have understood it, it is the fastest.

here is the XML:

<?xml version="1.0"?>

<stores>
<store name = "the Name of the Store">
    <category>2nd Hand</category>
    <locationAdress>sonstwo 18b, 12345 Bla</locationAdress>
    <telephoneNumber>1234567812</telephoneNumber>
    <openingHours>
        <!-- Opening Hours, day permitted values: 1(Monday) - 7(Sunday) and 8 (workweek) -->
        <openingTime day = "1">7:00 - 13:00 15:00 - 18:00</openingTime>
        <openingTime day = "2">7:00 - 13:00 15:00 - 18:00</openingTime>
        <openingTime day = "3">7:00 - 13:00 15:00 - 18:00</openingTime>
        <openingTime day = "4">7:00 - 13:00 15:00 - 18:00</openingTime>
        <openingTime day = "5">7:00 - 13:00 15:00 - 18:00</openingTime>
    </openingHours>
</store>
    </stores>

here is my java-code:

NodeList hoursList = storeElement.getElementsByTagName("openingHours").item(0).getChildNodes();
            for (int y = 0; y < hoursList.getLength(); y++) {
                Node hoursNode = hoursList.item(y);
                boolean isElement = hoursNode.getNodeType() == Node.ELEMENT_NODE;
                boolean hasChild = hoursNode.getChildNodes().getLength() != 0;
                String nameParent = hoursNode.getParentNode().getNodeName();

boolean isElement is false, hasChild also false and String nameParent is openingHours.

Leander
  • 1,322
  • 4
  • 16
  • 31
  • 2
    isElement should be false for some elements, and true for others, because openingHours has both child text nodes and child elements. Why don't you use `hoursList.getElementsByTagName("openingTime")`, since that's what you want? Also, DOM is far from being the fastest. – JB Nizet Mar 07 '13 at 22:48
  • Hmmm...then I think I understood the DOM-documentation wrong. I thought a Element ecapsulates a TextNode (my source http://www.w3schools.com/dom/dom_nodes.asp last part). Thanks ;) I haven´t read much about xml-parsing, since this is the only time I need to use it in my current project. I thought DOM is the fastest for smaller XML-files (comparing to SAX and Stax). – Leander Mar 07 '13 at 23:43
  • I'll try it tomorrow.....it's pretty late where I live :) – Leander Mar 08 '13 at 00:12
  • 1
    @Lele - If you mean this: "The way to get the text of an element is to get the value of the child node (text node).", then yes it's both wrong and confusing. It's only kind of true-ish in the degenerative case where the only contents of the element is a single text node. Even so it's the text of *the contents* of the element rather than the text of the element itself. W3Schools is a lousy source. See [w3fools.com](http://w3fools.com) for more details. – Alohci Mar 08 '13 at 00:46
  • I think I learned that by hard :D. Never trust W3Schools..... – Leander Mar 08 '13 at 14:11

0 Answers0