Unexpected #text-Nodes in a dom structure, parsed with jaxp

O

ovm

Hi,

i have a small config-file and after i haved parsed the file into a
dom-tree, i have unexpected nodes (#text) while walking through the
structure.

--the xml file--
<?xml version="1.0" encoding="utf-8" ?>
<classmappings>
<mapping>
<javatype>User</javatype>
<dbtype>User</dbtype>
<pkcolumns>
<col sequenced="true">UserID</col>
</pkcolumns>
</mapping>
<mapping>
<javatype>Group</javatype>
<dbtype>Group</dbtype>
<pkcolumns>
<col sequenced="true">GroupID</col>
</pkcolumns>
</mapping>
</classmappings>
--xml--

--the java program--
package xmltest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class Runner {
/**
* @param args
*/
static Document document;
public static void main(String[] args) {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse("db-mappings.xml");
} catch (Exception parserExp) {
System.out.println(
"Exception : " + parserExp.getMessage());
}
NodeList list = document.getElementsByTagName( "mapping" );
for ( int i = 0; i < list.getLength(); i++ ) {
Node node = list.item(i);
System.out.println( "Node no " + i + " : " +
node.getNodeName() );
NodeList list2 = node.getChildNodes( );
for ( int y = 0; y < list2.getLength(); y++ ) {
node = list2.item(y);
System.out.println(" Node no " + y + " : " +
node.getNodeName() + ":" + node.getFirstChild() );
}
}
}
}
--java--

and the output: ( why are those #text:null entries there? )
Node no 0 : mapping
Node no 0 : #text:null
Node no 1 : javatype:User
Node no 2 : #text:null
Node no 3 : dbtype:User
Node no 4 : #text:null
Node no 5 : pkcolumns:

Node no 6 : #text:null
Node no 1 : mapping
Node no 0 : #text:null
Node no 1 : javatype:Group
Node no 2 : #text:null
Node no 3 : dbtype:Group
Node no 4 : #text:null
Node no 5 : pkcolumns:

Node no 6 : #text:null

Can anyone show me a simpler way to get the desired configuration
entries? And, what are those #text-nodes? Should i use jdom?

I expected to get a structure like this
root-node ( classmappings )
child-node ( mapping )
child-node ( java-type ) : content ( User )
child-node ( dbtype ) : content ( User )
 
S

Stefan Schulz

i have a small config-file and after i haved parsed the file into a
dom-tree, i have unexpected nodes (#text) while walking through the
structure.

You do not provide a DTD, therefore the parser can not be sure if the
white space between your tags is meaningful, or if it is an artifact of
representation. It therefore includes it in the DOM Tree, producing text
nodes containing only whitespace.
 
O

ovm

ok, thanks. This works:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<!DOCTYPE classmappings [
<!ELEMENT col (#PCDATA)>
<!ELEMENT pkcolumns ( col+ )>
<!ELEMENT dbtype (#PCDATA)>
<!ELEMENT javatype (#PCDATA)>
<!ELEMENT mapping ( javatype, dbtype, pkcolumns )>
<!ELEMENT classmappings ( mapping+ )>
]>
..
..
..

Had to activate

factory.setValidating(true); and
factory.setIgnoringElementContentWhitespace(true);

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top