Imposible parsing XML in Xlet
Hi all,
I'm trying to make a parser to read an XML file from my XLET application, but I have problems. Could it be a problem of filereader?
I use Xlet View 0.3.6 to emule my application.
The parser is nanoxml.2.2.3.
--
The XML file I want to read is the following one:
<bookCollection>
<!-- Title and collection name -->
<collectionName>My Tech Book Collection</collectionName>
<collectionOwner>Lapo</collectionOwner>
<!-- The list of books goes here -->
<bookList>
<book title="Head First Design Patterns"
author="Freeman, Freeman"
year="2004"
publisher="O'Reilly"
/>
<book title="Thinking in Java"
author="B.Eckel"
year="2003"
publisher="Prentice Hall"
/>
</bookList>
</bookCollection>
-
Below is the code I am using:
import java.awt.*;
import javax.tv.xlet.*;
import org.havi.ui.HScene;
import org.havi.ui.HSceneFactory;
import org.havi.ui.HSceneTemplate;
import java.util.Enumeration;
import net.n3.nanoxml.IXMLElement;
import net.n3.nanoxml.IXMLParser;
import net.n3.nanoxml.IXMLReader;
import net.n3.nanoxml.StdXMLReader;
import net.n3.nanoxml.XMLParserFactory;
public class Xlet_XML extends Component implements javax.tv.xlet.Xlet
{
private XletContext context; // context the xlet is running in
private HScene scene; // The HScene which contains this xlet
private String message = "Xlet_XML"; // message to display
IXMLParser xmlParser;
IXMLReader xmlReader;
public Xlet_XML()
{
}
public void initXlet (javax.tv.xlet.XletContext context)throws javax.tv.xlet.XletStateChangeException
{
this.context = context;
HSceneFactory factory = HSceneFactory.getInstance();
HSceneTemplate hst = new HSceneTemplate();
hst.setPreference(HSceneTemplate.SCENE_SCREEN_DIMENSION, new org.havi.ui.HScreenDimension(1,1), HSceneTemplate.REQUIRED);
hst.setPreference(HSceneTemplate.SCENE_SCREEN_LOCATION, new org.havi.ui.HScreenPoint(0,0), HSceneTemplate.REQUIRED);
scene = factory.getBestScene(hst);
Rectangle rect = scene.getBounds();
setBounds(rect);
scene.setVisible(false);
scene.add(this);
}
public void startXlet() throws javax.tv.xlet.XletStateChangeException
{
scene.setVisible(true);
}
public void pauseXlet()
{
}
public void destroyXlet (boolean unconditional) throws javax.tv.xlet.XletStateChangeException
{
}
public void paint(Graphics g)
{
g.setFont(new Font("Tiresias",Font.BOLD, 36));
g.setColor(Color.white);
g.drawString(message, 275, 50);
IXMLElement book;
try
{
// Create the XML parser
g.drawString("primero", 275, 100);
xmlParser = XMLParserFactory.createDefaultXMLParser();
g.drawString("segundo", 275, 150);
xmlReader = StdXMLReader.fileReader("books.xml");
g.drawString("tercero", 275, 200);
xmlParser.setReader(xmlReader);
g.drawString("cuarto", 275, 250);
// Read file and parse it!
IXMLElement xmlDoc = (IXMLElement) xmlParser.parse();
// Get the tag called
IXMLElement node = xmlDoc.getFirstChildNamed("collectionName");
g.drawString("Collection Name: " + node.getContent(),275,300);
// Get the tag called
node = xmlDoc.getFirstChildNamed("collectionOwner");
g.drawString("Collection Owner: " + node.getContent(),275,350);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
-
When I execute my application, The XletView only shows:
Xlet_XML
primero
segundo
-
Can someone tell me what is going wrong?
Thanks,
rulito

