R
Ross
I'm having a mysterious problem reading XML files. For some reason,
Element.getChildNodes() works some of the time but not others.
Here's a simple XML file:
[test4.xml]
<document>
<blah>
word
</blah>
<blah>
word2
</blah>
<blah>
word3
</blah>
</document>
[end of test4.xml]
Here is a simple XML reading program, written to try to isolate the
error.
[TestXML.java]
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.util.*;
public class TestXML
{
public TestXML( File f ) throws IOException
{
Document doc = null;
try
{
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
FileInputStream fis = new FileInputStream( f );
doc = builder.parse( fis );
fis.close();
}
catch( IOException ioex )
{
throw ioex;
}
catch( Exception e )
{
e.printStackTrace();
throw new IOException( e.getMessage() );
}
print( doc.getDocumentElement(), 0 );
}
public void print( Element e, int tab )
{
put( tab );
System.out.println( "<" + e.getTagName() + ">" );
NodeList nl = e.getChildNodes();
for ( int index = 0; index < nl.getLength(); index++ )
{
Node n = nl.item( 0 );
put( tab + 8 );
System.out.println( "[Node type " + n.getNodeType() + "]" );
if ( n instanceof Text )
{
Text t = (Text) n;
put( tab + 8 );
System.out.print( t.getData() );
}
else if ( n instanceof Element )
{
Element e2 = (Element) n;
print( e2, tab + 8 );
}
}
System.out.println( "</" + e.getTagName() + ">" );
}
private void put( int tab )
{
for ( int index = 0; index < tab; index++ )
{
System.out.print( " " );
}
}
public static void main( String args[] ) throws IOException
{
TestXML tx = new TestXML( new File( args[0] ));
}
}
[end of TestXML.java]
But, when I run the program on the xml file, it doesn't find any of
the "blah" nodes.
[output]
<document>
[Node type 3]
[Node type 3]
[Node type 3]
[Node type 3]
[Node type 3]
[Node type 3]
[Node type 3]
</document>
[end of output]
Any advice? I just can't see what the error is. (Hopefully it won't be
another case of me spotting the error as soon as I post here
Element.getChildNodes() works some of the time but not others.
Here's a simple XML file:
[test4.xml]
<document>
<blah>
word
</blah>
<blah>
word2
</blah>
<blah>
word3
</blah>
</document>
[end of test4.xml]
Here is a simple XML reading program, written to try to isolate the
error.
[TestXML.java]
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.util.*;
public class TestXML
{
public TestXML( File f ) throws IOException
{
Document doc = null;
try
{
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
FileInputStream fis = new FileInputStream( f );
doc = builder.parse( fis );
fis.close();
}
catch( IOException ioex )
{
throw ioex;
}
catch( Exception e )
{
e.printStackTrace();
throw new IOException( e.getMessage() );
}
print( doc.getDocumentElement(), 0 );
}
public void print( Element e, int tab )
{
put( tab );
System.out.println( "<" + e.getTagName() + ">" );
NodeList nl = e.getChildNodes();
for ( int index = 0; index < nl.getLength(); index++ )
{
Node n = nl.item( 0 );
put( tab + 8 );
System.out.println( "[Node type " + n.getNodeType() + "]" );
if ( n instanceof Text )
{
Text t = (Text) n;
put( tab + 8 );
System.out.print( t.getData() );
}
else if ( n instanceof Element )
{
Element e2 = (Element) n;
print( e2, tab + 8 );
}
}
System.out.println( "</" + e.getTagName() + ">" );
}
private void put( int tab )
{
for ( int index = 0; index < tab; index++ )
{
System.out.print( " " );
}
}
public static void main( String args[] ) throws IOException
{
TestXML tx = new TestXML( new File( args[0] ));
}
}
[end of TestXML.java]
But, when I run the program on the xml file, it doesn't find any of
the "blah" nodes.
[output]
<document>
[Node type 3]
[Node type 3]
[Node type 3]
[Node type 3]
[Node type 3]
[Node type 3]
[Node type 3]
</document>
[end of output]
Any advice? I just can't see what the error is. (Hopefully it won't be
another case of me spotting the error as soon as I post here