1

I have the following xml file:

<TEST>
<NEED ID="0">OK</NEED>
</TEST>

I simply want to get the attribute of the tag NEED and save it into an Integer variable.

@XmlRootElement(name = "TEST")
@XmlAccessorType (XmlAccessType.FIELD)
public class Resp {

    @XmlAttribute(name = "ID", required = true) 
    protected Integer resultId;
    // getters, setters, constructor
}

How to receive the ID value?

ninjaxelite
  • 1,139
  • 2
  • 20
  • 43

1 Answers1

0

You can use JDom parser.

File inputFile = new File("input.xml");
SAXBuilder saxBuilder = new SAXBuilder();

Document document = saxBuilder.build(inputFile);
Element classElement = document.getRootElement();

List<Element> aList = classElement.getChildren();


for (int temp = 0; temp < aList.size(); temp++) {    
            Element student = aList.get(temp);

            Attribute attribute =  student.getAttribute("ID");
            System.out.println("ID : " 
               + attribute.getValue() ); 

         }
aru007
  • 370
  • 3
  • 11