【问题标题】:How to get the root element in xml using Android?如何使用Android获取xml中的根元素?
【发布时间】:2013-01-03 17:25:14
【问题描述】:

仅使用 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);

【问题讨论】:

    标签: java android xml soap


    【解决方案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();
    

    【讨论】:

    • 我正在使用 Ksoap2 并尝试使用 SAX 进行解析。 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();文档生成器数据库; db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xml));文档 doc = db.parse(is);
    猜你喜欢
    • 1970-01-01
    • 2019-03-02
    • 2015-01-31
    • 1970-01-01
    • 2014-02-19
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多