Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving problems reading an XML file in Java and getting Element attributes
    text
    copied!<p>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.</p> <p>A sample of the XML is</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt; &lt;photostack3d&gt; &lt;photos&gt; &lt;photo&gt; &lt;thumb src="Thumbs/bed1.jpg"/&gt; &lt;img src="Photos/bed1.jpg"/&gt; &lt;caption text="Master Bedroom"/&gt; &lt;desc&gt;&lt;![CDATA[&lt;h1&gt;Master Bedroom&lt;/h1&gt;&lt;br&gt;The master bedroom is roomy and has a beautiful view of the landscaped back yard.]]&gt;&lt;/desc&gt; &lt;/photo&gt; &lt;/photos&gt; &lt;/photostack3d&gt; </code></pre> <p>In this XML, there can be multiple photo nodes, since those define each of the photos that will be displayed by the gallery...</p> <p>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:</p> <pre class="lang-java prettyprint-override"><code>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 &lt; 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(); } } </code></pre> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload