Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何在特定xml标记下获取值_Android_Xml_Parsing_Url - Fatal编程技术网

Android:如何在特定xml标记下获取值

Android:如何在特定xml标记下获取值,android,xml,parsing,url,Android,Xml,Parsing,Url,我想在我的xml文件(通过url访问)的特定标记下获取值,如下所示: <SyncLoginResponse> <Videos> <Video> <name>27/flv</name> <title>scooter</title> <url>http://dev2.somedomain.com/tabletcms/tablets/table

我想在我的xml文件(通过url访问)的特定标记下获取值,如下所示:

<SyncLoginResponse>
    <Videos>
    <Video>
        <name>27/flv</name>
        <title>scooter</title>
        <url>http://dev2.somedomain.com/tabletcms/tablets/tablet_content/000002/videos/</url>
        <thumbnail>http://dev2.somedomain.com/tabletcms/tablets/tablet_content/000002/thumbnails/106/jpg</thumbnail>

    </Video>
    <Video>
        <name>26/flv</name>
        <title>barsNtone</title>
        <url>http://dev2.somedomain.com/tabletcms/tablets/tablet_content/000002/videos/</url>
        <thumbnail>http://dev2.somedomain.com/tabletcms/tablets/tablet_content/000002/thumbnails/104/jpg</thumbnail>
    </Video>

    </Videos>
    <Slideshows>
    <Slideshow>
        <name>44</name>
        <title>ProcessFlow</title>
        <pages>4</pages>
        <url>http://dev2.somedomain.com/tabletcms/tablets/tablet_content/000002/slideshows/</url>

    </Slideshow>
    </Slideshows>
<SyncLoginResponse>
上述代码在UI中返回以下结果:

...PARSE AND DOWNLOAD...
Name:> 27/flv
Title:> scooter
Pages:> 4
File:> http://dev2.somedomain.com/tabletcms/tablets/tablet_content/000002/videos/
我希望它是:

...PARSE AND DOWNLOAD...
Name:> 44
Title:> ProcessFlow
Pages:> 4
File:> http://dev2.somedomain.com/tabletcms/tablets/tablet_content/000002/slideshows/

为父标记保留布尔变量

并在EndElement()方法中检查父标记是否为true

如果该值为真,则将该特定值存储到相应的变量中

e、 g

如果你有

<ResultSet>
<result>
<name>a</name>
</result>
<customresult>
<name>avs</customresult>
</ResultSet>

A.
avs
取两个变量作为结果&布尔型customresult

最初将它们标记为false

在startElement()方法中检查解析是否以Result元素开始,如果是这样的话

然后把它标记为真的

&现在您知道您正在解析结果,因此可以轻松地识别

结果

致以最良好的祝愿


~Anup

尝试使用DOM解析器而不是SAX解析器

input=httpconnection.openHttpConnection(url);
            isr = new InputStreamReader(input);
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
            document = documentBuilder.parse(input, null);
            document.getDocumentElement().normalize();
            NodeList videosList = document.getElementsByTagName("Video");
            for (int i = 0; i < videosList .getLength(); i++) 
            {
                         Node node = videosList .item(i);
                Element playlistElement = (Element) node;
                NodeList title = playlistElement.getElementsByTagName("name");
                if (title.item(0).getChildNodes().item(0) != null) 
                {   
                    Element nameElement = (Element) title.item(0);
                    title = nameElement.getChildNodes();
                    Log.v("name===>",((Node) title.item(0)).getNodeValue());
                }

//Like wise use the same code for title ,url ,thumbnail...
            }
input=httpconnection.openHttpConnection(url);
isr=新输入流读取器(输入);
DocumentBuilderFactory DocumentBuilderFactory=DocumentBuilderFactory.newInstance();
documentBuilder=documentBuilderFactory.newDocumentBuilder();
document=documentBuilder.parse(输入,空);
document.getDocumentElement().normalize();
NodeList videosList=document.getElementsByTagName(“视频”);
对于(int i=0;i”,((节点)title.item(0)).getNodeValue();
}
//像wise一样,对标题、url、缩略图使用相同的代码。。。
}
公共类XMLHandler扩展了DefaultHandler
{
布尔结果;
布尔结果;
字符串温度;
@凌驾
public void startElement(字符串uri、字符串localName、字符串qName、属性)引发SAXException
{
else if(localName.equalsIgnoreCase(“结果”))
{
结果=真;
}
else if(localName.equalsIgnoreCase(“customresult”))
{
customresult=true;
}
}
@凌驾
public void endElement(字符串uri、字符串localName、字符串qName)引发SAXException{
/**设定值*/
if(localName.equalsIgnoreCase(“name”)&&result==true)
{
String name=temp;//这只是结果标记中的名称
}
else if(localName.equalsIgnoreCase(“name”)&&result==true)
{
String name=temp;//这是customresult标记中的名称
}
else if(localName.equalsIgnoreCase(“结果”))
{
结果=假;
}
else if(localName.equalsIgnoreCase(“customresult”))
{
customresult=false;
}
}
/**调用以获取标记字符(例如:-AndroidPeople
*——获得男性化(人物性格)*/
@凌驾
公共无效字符(char[]ch,int start,int length)引发异常
{
temp=新字符串(ch、开始、长度);
}
}

这就是我所有的朋友,如果你觉得它有用,那么别忘了将它标记为答案。

很抱歉,我的问题是noob,但是,你有关于如何在我的案例中设置这些布尔变量的示例代码吗?谢谢您的回复!:)不要使用DOM解析器和SAX解析器,它们都会让生活变得更加艰难。只需使用简单的XML框架即可。我非常喜欢它,我在上面写了一篇博文:非常感谢你,我的朋友!你救了我一天!:)
...PARSE AND DOWNLOAD...
Name:> 44
Title:> ProcessFlow
Pages:> 4
File:> http://dev2.somedomain.com/tabletcms/tablets/tablet_content/000002/slideshows/
<ResultSet>
<result>
<name>a</name>
</result>
<customresult>
<name>avs</customresult>
</ResultSet>
input=httpconnection.openHttpConnection(url);
            isr = new InputStreamReader(input);
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
            document = documentBuilder.parse(input, null);
            document.getDocumentElement().normalize();
            NodeList videosList = document.getElementsByTagName("Video");
            for (int i = 0; i < videosList .getLength(); i++) 
            {
                         Node node = videosList .item(i);
                Element playlistElement = (Element) node;
                NodeList title = playlistElement.getElementsByTagName("name");
                if (title.item(0).getChildNodes().item(0) != null) 
                {   
                    Element nameElement = (Element) title.item(0);
                    title = nameElement.getChildNodes();
                    Log.v("name===>",((Node) title.item(0)).getNodeValue());
                }

//Like wise use the same code for title ,url ,thumbnail...
            }
public class XMLHandler extends DefaultHandler 
{
    boolean result;
boolean customresult;
    String temp;

    @Override
    public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException 
    {


        else if (localName.equalsIgnoreCase("result"))
        {
            result = true;
        }
        else if(localName.equalsIgnoreCase("customresult"))
        {
            customresult =true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)throws SAXException {

        /** set value */ 
        if (localName.equalsIgnoreCase("name") && result == true)
        {
                  String name = temp; //which is nothing but your name from result tag

        }
        else if(localName.equalsIgnoreCase("name") && result == true)
        {
            String name = temp; //which is nothing but your name from customresult tag
        }
            else if(localName.equalsIgnoreCase("result"))
        {
                 result = false;
        }
            else if(localName.equalsIgnoreCase("customresult"))
        {
                 customresult = false;
        }
    }

    /** Called to get tag characters ( ex:- <name>AndroidPeople</name> 
     * -- to get AndroidPeople Character ) */
    @Override
    public void characters(char[] ch, int start, int length)throws SAXException
    {
        temp = new String(ch, start, length);
    }


}