1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
private Object result;
private ArrayList<TypeInterpretation> interpretation = new ArrayList<TypeInterpretation>() ;
//les constructeurs et les getters et les setters
public ArrayList<TypeInterpretation> getInterpretation(String path,String id)
{
//Creation of our factory
XMLInputFactory factory = XMLInputFactory.newInstance();
//give path
File file = new File(path);
try {
//get reader
XMLStreamReader reader = factory.createXMLStreamReader(new FileReader(file));
while (reader.hasNext()) {
// get the event
int type = reader.next();
if(type== XMLStreamReader.START_ELEMENT && reader.getLocalName()=="attribute" && reader.getAttributeName(0).toString() == "id" && reader.getAttributeValue(0).equals(id))
{ Boolean found=false ;
while (reader.hasNext()&& !found)
{
type = reader.next();
switch (type)
{
case XMLStreamReader.START_ELEMENT:
{
String name = reader.getLocalName();
int nbrOfAtt = reader.getAttributeCount();
Map<String,String> attri = new HashMap<>(nbrOfAtt);
for(int i = 0; i < reader.getAttributeCount(); i++)
{
attri.put(reader.getAttributeName(i).toString(), reader.getAttributeValue(i)) ;
}
TypeInterpretation ob= new TypeInterpretation(name, nbrOfAtt, attri);
this.interpretation.add(ob);
break;
}
case XMLStreamReader.END_ELEMENT:
if(reader.getLocalName()=="attribute")
{
found=true;
}
break;
}
}
}
else
//attribute is not found
interpretation=null;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}
return interpretation ;
} |