Failing to get element values using Element.getAttribute()

后端 未结 4 512
北恋
北恋 2021-01-14 05:14

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 r

4条回答
  •  广开言路
    2021-01-14 06:08

    Use dom4j library.

    InputStream is = new FileInputStream(filePath);
    
    SAXReader reader = new SAXReader();
    org.dom4j.Document doc = reader.read(is);
    is.close();
    Element content = doc.getRootElement();  //this will return the root element in your xml file
    List methodEls = content.elements("element"); // this will retun List of all Elements with name "element"
    Attribute attrib = methodEls.get(0).attribute("attributeName"); // this is the "attributeName" attribute of first element with name "element"
    

提交回复
热议问题