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 )
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 )