Why are there # text nodes in my XML file?

I am making an Android application that parses the DOM in 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 the dom parser, for example:

 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()); } } } 

The following is printed in my application

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 these #text fields are and, more importantly, how can I get rid of them?

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

+6
source share
3 answers

These are spaces (newlines, spaces, tabs) :)

+5
source

This is what you get:

1) List node with all nodes being grand-parents

 NodeList nodd = doc.getElementsByTagName("grandparent"); 

2) All children of the grand parent node x

 NodeList nodes = node.getChildNodes(); 

which are auxiliary nodes

 < grandparent > < parent1 > ... < /parent1 > < parent2 > ... < /parent2 > < /grandparent > 

3) Baby y

 nodes.item(y); 

There may be text between the text and this will be #text if you have:

 < grandparent > yourTextHere1 < parent1 > ... < /parent1 > yourTextHere2 < parent2 > ... < /parent2 > yourTextHere3 < /grandparent > 

You'll get:

yourTextHere1 Parent1 yourTextHere2 parent2 yourTextHere3

I hope this helps you! Julien

+1
source

Do this when analyzing a document,

 Document doc = builder.parse(input); doc.getDocumentElement().normalize(); 

This will cancel the xml file and delete all unwanted #text files.

0
source

Source: https://habr.com/ru/post/893194/


All Articles