在 Android 中解析 XML

发布于 2024-11-03 22:06:00 字数 2594 浏览 3 评论 0原文

我需要解析这个 XML:

    <WhoisRecord xmlns="http://adam.kahtava.com/services/whois" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<DomainName>68.140.1.1</DomainName>
<RegistryData><AbuseContact><Email>[email protected]</Email><Name>abuse</Name><Phone>+1-800-900-0241</Phone></AbuseContact><AdministrativeContact><Email>[email protected]</Email><Name>Verizon Internet Services</Name><Phone>800-243-6994</Phone></AdministrativeContact><BillingContact i:nil="true"/><CreatedDate>2002-05-13T00:00:00-04:00</CreatedDate><RawText i:nil="true"/><Registrant><Address>22001 Loudoun County Parkway</Address><City>Ashburn</City><Country>US</Country><Name>UUNET Technologies, Inc.</Name><PostalCode>20147</PostalCode><StateProv>VA</StateProv></Registrant><TechnicalContact><Email>[email protected]</Email><Name>swipper</Name><Phone>+1-800-900-0241</Phone></TechnicalContact><UpdatedDate>2004-03-16T00:00:00-05:00</UpdatedDate><ZoneContact i:nil="true"/></RegistryData></WhoisRecord>

我的代码如下所示:

public class XMLParser 
{


    String streamTitle = "";

    /** Called when the activity is first created. 
     * @throws IOException 
     * @throws SAXException */
    public String startparse(String xml) throws SAXException, IOException 
    {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

    try 
    {
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        builder.parse(xml);
    } 
    catch (ParserConfigurationException e) 
    {
        e.printStackTrace();  
    }

    return builderFactory.getAttribute("WhoisRecord").toString();

    }
}

当我尝试从 startparse 返回某些内容时,我什么也没得到。

 XMLParser xmlpar = new XMLParser();
Log.v("Faruk TEST ", "udss:"+xmlpar.startparse(temp));

有人知道解决这个问题的简单方法吗?

Hy i need to parse this XML :

    <WhoisRecord xmlns="http://adam.kahtava.com/services/whois" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<DomainName>68.140.1.1</DomainName>
<RegistryData><AbuseContact><Email>[email protected]</Email><Name>abuse</Name><Phone>+1-800-900-0241</Phone></AbuseContact><AdministrativeContact><Email>[email protected]</Email><Name>Verizon Internet Services</Name><Phone>800-243-6994</Phone></AdministrativeContact><BillingContact i:nil="true"/><CreatedDate>2002-05-13T00:00:00-04:00</CreatedDate><RawText i:nil="true"/><Registrant><Address>22001 Loudoun County Parkway</Address><City>Ashburn</City><Country>US</Country><Name>UUNET Technologies, Inc.</Name><PostalCode>20147</PostalCode><StateProv>VA</StateProv></Registrant><TechnicalContact><Email>[email protected]</Email><Name>swipper</Name><Phone>+1-800-900-0241</Phone></TechnicalContact><UpdatedDate>2004-03-16T00:00:00-05:00</UpdatedDate><ZoneContact i:nil="true"/></RegistryData></WhoisRecord>

My code looks like this:

public class XMLParser 
{


    String streamTitle = "";

    /** Called when the activity is first created. 
     * @throws IOException 
     * @throws SAXException */
    public String startparse(String xml) throws SAXException, IOException 
    {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

    try 
    {
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        builder.parse(xml);
    } 
    catch (ParserConfigurationException e) 
    {
        e.printStackTrace();  
    }

    return builderFactory.getAttribute("WhoisRecord").toString();

    }
}

when i try to return something from startparse i simple get nothing.

 XMLParser xmlpar = new XMLParser();
Log.v("Faruk TEST ", "udss:"+xmlpar.startparse(temp));

Do some one know a simple solution for this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情释 2024-11-10 22:06:00

试试这个,我希望这是您正在寻找的...

Android:从字符串中解析XML问题

Try this i hope it is what you are looking for ...

Android: parse XML from string problems

似梦非梦 2024-11-10 22:06:00

我认为您对 builderFactory.getAttribute 的调用是错误的。

DocumentBuilder.parse() 返回一个 Document 对象,它将包含您刚刚解析的 DOM。您可以使用它来访问 XML 的元素。

I think that your call to builderFactory.getAttribute is wrong.

DocumentBuilder.parse() returns a Document object, and this will contain the DOM that you've just parsed. You can use this to access the elements of the XML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文