Osh
Osh

Reputation: 119

Having problems reading an XML file in Java and getting Element attributes

My objective is to read an XML file and provide a simple interface that will enable a (non-technical) user to modify this file. The xml file drives a Flash photo gallery and is pre-defined by that Flash Actionscript.

A sample of the XML is

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<photostack3d>
    <photos>
        <photo>
            <thumb src="Thumbs/bed1.jpg"/>
            <img src="Photos/bed1.jpg"/>
            <caption text="Master Bedroom"/>
            <desc><![CDATA[<h1>Master Bedroom</h1><br>The master bedroom is roomy and has a beautiful view of the landscaped back yard.]]></desc>
        </photo>
    </photos>
</photostack3d>

In this XML, there can be multiple photo nodes, since those define each of the photos that will be displayed by the gallery...

Now, I'm at the point where I am using DOM to create the file, so we're good there. Using DOM to try and read it in for further editing is where I'm running into a problem. I can get to all of the photo elements, but I'm having a problem getting at the attributes in there, namely thumb, img, caption and desc. Currently, I have the following:

private void loadXML(String filePath)
{
    try 
    {
        File fXmlFile = new File(filePath);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        NodeList photosList = doc.getElementsByTagName("photos");
        System.out.println("-----------------------");

        NodeList photoList = doc.getElementsByTagName("photo");
        System.out.println("Number of photo nodes: " + photoList.getLength());

        for (int temp = 0; temp < photoList.getLength(); temp++) 
        {

            NodeList thumbList = doc.getElementsByTagName("thumb");
            Element thumbElement = (Element) thumbList.item(0);

            String thumbName = thumbElement.getAttribute("thumb");
            System.out.println("thumb name: " + thumbName);

            //Node nNode = photoList.item(temp);
            //if (nNode.getNodeType() == Node.ELEMENT_NODE) 
            //{

            //  Element eElement = (Element) nNode;

            //  System.out.println("Source Name : " + eElement.getAttribute("text"));     //.getElementsByTagName("thumb"));
                //System.out.println("Source Name : " + getTagValue("thumb", eElement));
                System.out.println("-----------------------");
            //}
        }
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
}

}

As you can see, I've been trying various different way to get at those attributes, but as of yet, just don't see the values coming back. Where am I going wrong?

Upvotes: 1

Views: 821

Answers (1)

Doug Ayers
Doug Ayers

Reputation: 1078

Have you tried thumbElement.getAttribute("src"); ?

Upvotes: 2

Related Questions