如何使用Android获取xml中的根元素?

时间:2013-01-03 17:25:14

标签: java android xml soap

仅使用代码Java我可以使用这些行获取根名称。

Element root = document.getDocumentElement();

并使用root.getNodeName()

获取名称

但是在Android环境中,我怎样才能获得名称'aluno'作为根名称?

<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>

更新(从下面的评论中复制):

我正在使用Ksoap2并尝试使用SAX进行解析。

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

1 个答案:

答案 0 :(得分:1)

在您的示例中,“aluno”显示为标记名称。如果您使用Jsoup,您可以按标记找到元素,然后使用tagName方法检索其名称:

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();