Note that there are some explanatory texts on larger screens.

plurals
  1. POFailing to get element values using Element.getAttribute()
    primarykey
    data
    text
    <p>I would like to read an xml file. I' ve found an example which is good until the xml element doesn't have any attributes. Of course i've tried to look after how could I read attributes, but it doesn't works.</p> <p>XML for example</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt; &lt;car&gt; &lt;properties&gt; &lt;test h="1.12" w="4.2"&gt; &lt;colour&gt;red&lt;/colour&gt; &lt;/test&gt; &lt;/properties&gt; &lt;/car&gt; </code></pre> <p>Java Code:</p> <pre><code>public void readXML(String file) { try { File fXmlFile = new File(file); DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); for (int temp = 0; temp &lt; nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("test : " + getTagValue("test", eElement)); System.out.println("colour : " + getTagValue("colour", eElement)); } } } catch (Exception e) { e.printStackTrace(); } } public String getTagValue(String sTag, Element eElement) { NodeList nlList = eElement.getElementsByTagName(sTag).item(0) .getChildNodes(); Node nValue = (Node) nlList.item(0); System.out.println(nValue.hasAttributes()); if (sTag.startsWith("test")) { return eElement.getAttribute("w"); } else { return nValue.getNodeValue(); } } </code></pre> <p>Output:</p> <p>false </p> <p>test :</p> <p>false</p> <p>colour : red</p> <p>My problem is, that i can't print out the attributes. How could i get the attributes?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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