Debora Martins
Debora Martins

Reputation: 243

How to get the root element in xml using Android?

Using only code Java I can get the root name with these lines.

Element root = document.getDocumentElement();

and get the name with root.getNodeName()

But in a Android enviroment, how can I get for example, the name 'aluno' as root name?

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns1:autenticaAlunoResponse xmlns:ns1="http://xfire.codehaus.org/AlunoService">
<aluno xmlns="urn:bean.wsi.br">
<matricula xmlns="http://bean.wsi.br">61203475</matricula>
<turma xmlns="http://bean.wsi.br"><codigo>2547</codigo>
<nome>B</nome>
</turma>
</aluno>
</ns1:autenticaAlunoResponse>
</soap:Body>
</soap:Envelope>

Update (copied from a comment below):

I'm using Ksoap2 and trying to parse using SAX.

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db; 
db = dbf.newDocumentBuilder(); 
InputSource is = new InputSource(); 
is.setCharacterStream(new StringReader(xml)); 
Document doc = db.parse(is);

Upvotes: 1

Views: 1670

Answers (1)

Marcin S.
Marcin S.

Reputation: 11191

In your example 'aluno' appears as a tag name. If you work with Jsoup you can find the element by tag and then use tagName method to retrieve its name:

Document doc;
Elements tagName;
String name;

try {
    doc = Jsoup.connect(url).userAgent("Mozilla").get();
} catch (IOException ioe) {
ioe.printStackTrace();
}

doc.select("aluno");
name = tagName.tagName();

Upvotes: 1

Related Questions