Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Dom getAttribute
    text
    copied!<p>im trying to parse xml file, but it wont print attribut value. I dont know how to get attribute typ from phone</p> <pre><code> try { String subor = "Noviny.xml"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(subor); System.out.println("----------------\n"); NodeList nodelist = document.getElementsByTagName("Author"); NodeList nodelist1 = document.getElementsByTagName("Article"); for(int i = 0; i &lt; nodelist.getLength(); i++) { Node uzol = nodelist.item(i); if (uzol.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) uzol; System.out.println("Id:" + element.getElementsByTagName("Id").item(0).getTextContent() + "\n"); System.out.println("Name:" + element.getElementsByTagName("Name").item(0).getTextContent() + "\n"); System.out.println("Email:" + element.getElementsByTagName("Email").item(0).getTextContent() + "\n"); System.out.println("typ: " + element.getAttribute("typ") + "\n"); System.out.println("phone:" + element.getElementsByTagName("phone").item(0).getTextContent() + "\n"); System.out.println("typ: " + element.getAttribute("typ") + "\n"); System.out.println("sal: " + element.getElementsByTagName("sal").item(0).getTextContent() + "\n"); } catch (Exception e) { e.printStackTrace(); } </code></pre> <p>and xml</p> <pre><code>&lt;Noviny&gt; &lt;Author&gt; &lt;Id&gt;1&lt;/Id&gt; &lt;Name&gt;first&lt;/Name&gt; &lt;Email&gt;first@gmail.com&lt;/Email&gt; &lt;phone typ="mobil"&gt;09443916565&lt;/phone&gt; &lt;sal&gt;500&lt;/sal&gt; &lt;/Author&gt; &lt;Author&gt; &lt;Id&gt;2&lt;/Id&gt; &lt;Name&gt;second&lt;/Name&gt; &lt;Email&gt;second@gmail.com&lt;/Email&gt; &lt;phone typ="pevna"&gt;094415665465&lt;/phone&gt; &lt;sal&gt;1000&lt;/sal&gt; &lt;/Author&gt; </code></pre> <p>and one more thing:ňIs it possible to just print (System.out.println) tree representation of xml file?</p> <p>thank you</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