Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there #text nodes in my xml file?

I'm making an android application that does DOM parsing on an xml file. I have an xml file that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<family>
    <grandparent>
        <parent1>
            <child1>Foo</child1>
            <child2>Bar</child2>
        </parent1>
        <parent2>
            <child1>Raz</child1>
            <child2>Mataz</child2>
        </parent2>
    </grandparent>  
</family>

If I run a dom parser on it, like this:

try {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        
    Document doc = builder.parse(input);
    doc.getDocumentElement().normalize();   //added in since the edit
    NodeList nodd = doc.getElementsByTagName("grandparent");
    for (int x = 0; x < nodd.getLength(); x++){
        Node node = nodd.item(x);
        NodeList nodes = node.getChildNodes();
        for(int y = 0; y < nodes.getLength(); y++){
            Node n = nodes.item(y);
            System.out.println(n.getNodeName());
        }
    }
}

My application prints out the following

07-20 18:24:28.395: INFO/System.out(491): #text

07-20 18:24:28.395: INFO/System.out(491): parent1

07-20 18:24:28.395: INFO/System.out(491): #text

07-20 18:24:28.395: INFO/System.out(491): parent2

07-20 18:24:28.395: INFO/System.out(491): #text

My question is, what are those #text fields and more importantly, how do I get rid of them?

Edit: So now that I know what they are, I tried to normalize it. I have updated the code to reflect the changes, but same result.

like image 257
Otra Avatar asked Jul 20 '11 18:07

Otra


People also ask

Why is there vs Why are there?

The choice between the phrases there is and there are at the beginning of a sentence is determined by the noun that follows it. Use there is when the noun is singular (“There is a cat”). Use there are when the noun is plural (“There are two cats”).

Why is there so many or why are there so many?

There ARE many is correct because you are talking about 'many' which is plural.

Is it a week or an WEEK?

A week is correct. Because the word “week” starts without a vowel sound. If the starting sonud is vowel, you must use “an” instead of “a”. The word “week” starts with a consonant sound.


1 Answers

It's whitespace (newlines, spaces, tabs) :)

like image 160
Ray Toal Avatar answered Sep 18 '22 06:09

Ray Toal