使用 XmlPullParser 处理 Xml 命名空间

发布于 2024-10-04 16:16:53 字数 2130 浏览 9 评论 0原文

这里我使用 XmlPullParser 来解析下面的文档。由于命名空间的原因它不起作用,如何解析命名空间?

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
XmlPullParser xpp = factory.newPullParser();

xpp.setInput(in, HTTP.UTF_8);
String namespace = xpp.getNamespace();

boolean inMessage = false;

int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
    //START TAGS
    if(eventType == XmlPullParser.START_TAG) {
        if(xpp.getName().equals(namespace+"resource")) {
            httpCode = Integer.valueOf( xpp.getAttributeValue(null, "code").trim() );
            type = xpp.getAttributeValue(null, "type").trim();
        } else if(xpp.getName().equals(namespace+"message")) {
            inMessage = true;
        }
    //TAG TEXT
    } else if(eventType == XmlPullParser.TEXT) {
        if(inMessage) {
            message = xpp.getText().trim();
            break; //CANCEL the iteration
        }
    } else if(eventType == XmlPullParser.END_TAG) {
        if(inMessage) {
            inMessage = false;
        }
    }
    eventType = xpp.next();
}

这是我想要解析的文档类型的示例,

<?xml version="1.0" encoding="UTF-8"?>
    <res:resource 
        xmlns:res="http://www.example.com/ns/server/resource"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.example.com/ns/server/resource resource.xsd "
        version="1" >
    <res:message httpCode="200" type="ok" >
        <![CDATA[Sample Success Response]]>
    </res:message>
    <dif:person 
        xmlns:dif="http://www.example.com/ns/server/resource"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.example.com/ns/server/person person.xsd "
        version="1" >
        <dif:name>test name</dif:name>
        <dif:description lang="en">test description</dif:description>
    </dif:person >
</res:resource>

我想分别解析 resdif

Here I am using XmlPullParser to parse the document below. It does not work because of the namespaces, how do I parse with namespaces?

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
XmlPullParser xpp = factory.newPullParser();

xpp.setInput(in, HTTP.UTF_8);
String namespace = xpp.getNamespace();

boolean inMessage = false;

int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
    //START TAGS
    if(eventType == XmlPullParser.START_TAG) {
        if(xpp.getName().equals(namespace+"resource")) {
            httpCode = Integer.valueOf( xpp.getAttributeValue(null, "code").trim() );
            type = xpp.getAttributeValue(null, "type").trim();
        } else if(xpp.getName().equals(namespace+"message")) {
            inMessage = true;
        }
    //TAG TEXT
    } else if(eventType == XmlPullParser.TEXT) {
        if(inMessage) {
            message = xpp.getText().trim();
            break; //CANCEL the iteration
        }
    } else if(eventType == XmlPullParser.END_TAG) {
        if(inMessage) {
            inMessage = false;
        }
    }
    eventType = xpp.next();
}

Here is an example of the sort of document I want to parse

<?xml version="1.0" encoding="UTF-8"?>
    <res:resource 
        xmlns:res="http://www.example.com/ns/server/resource"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.example.com/ns/server/resource resource.xsd "
        version="1" >
    <res:message httpCode="200" type="ok" >
        <![CDATA[Sample Success Response]]>
    </res:message>
    <dif:person 
        xmlns:dif="http://www.example.com/ns/server/resource"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.example.com/ns/server/person person.xsd "
        version="1" >
        <dif:name>test name</dif:name>
        <dif:description lang="en">test description</dif:description>
    </dif:person >
</res:resource>

I want to parse res and dif separately.

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

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

发布评论

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

评论(1

中二柚 2024-10-11 16:16:53

您可以通过调用 getPrefix 方法来区分它们。一些测试代码:

try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();                     
        InputStream input = getResources().openRawResource(R.raw.test);
        parser.setInput(input, HTTP.UTF_8);     
        int eventType = parser.getEventType();
        while(eventType != XmlPullParser.END_DOCUMENT){             
            if(eventType == XmlPullParser.START_TAG){ 
                String prefix = parser.getPrefix();
                String name   = parser.getName();
                Log.i("XML", String.format("prefix=%s,name=%s",prefix,name));
                if( "dif".equals(prefix)){
                    handleDiff(parser);
                }else if ("res".equals(prefix)){
                    handleRes(parser);
                }
            }

            eventType = parser.next();
        }
    } catch (Exception e) {
        Log.e("XML","",e);
    }  

Logcat 输出您的测试数据:

prefix=res,name=resource  
prefix=res,name=message  
prefix=dif,name=person  
prefix=dif,name=name  
prefix=dif,name=description  

You can distinguish them by call to getPrefix method. Some test code:

try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();                     
        InputStream input = getResources().openRawResource(R.raw.test);
        parser.setInput(input, HTTP.UTF_8);     
        int eventType = parser.getEventType();
        while(eventType != XmlPullParser.END_DOCUMENT){             
            if(eventType == XmlPullParser.START_TAG){ 
                String prefix = parser.getPrefix();
                String name   = parser.getName();
                Log.i("XML", String.format("prefix=%s,name=%s",prefix,name));
                if( "dif".equals(prefix)){
                    handleDiff(parser);
                }else if ("res".equals(prefix)){
                    handleRes(parser);
                }
            }

            eventType = parser.next();
        }
    } catch (Exception e) {
        Log.e("XML","",e);
    }  

Logcat out put for your test data:

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