获取从一个XML文件中的随机项目文件、项目、XML

2023-09-04 02:59:27 作者:温一蛊酒

我已经建立2 XML文件:

I have created 2 xml files:

RES / XML / comedy.xml

<comedy>
<item title="Grown Ups" length="90min"/>
<item title="Grown Ups 2" length="90min"/>
<item title="Scary Movie" length="90min"/>
<item title="Superbad" length="90min"/>
<item title="Zoolander" length="90min"/>
<item title="Groundhog Day" length="90min"/>
<item title="Beverly Hills Cop" length="90min"/>
<item title="Beverly Hills Cop II" length="90min"/>
<item title="Hangover" length="90min"/>
<item title="Anchorman" length="90min"/>
<item title="Pineapple Express" length="90min"/>
<item title="Happy Gilmore" length="90min"/>
<item title="We're the Millers" length="90min"/>
<item title="Horrible Bosses" length="90min"/>
<item title="Horrible Bosses 2" length="90min"/>
<item title="Meet the Parents" length="90min"/>

RES / XML / action.xml

<action>
<item title="Man in Black" length="90min"/>
<item title="Man in Black II" length="90min"/>
<item title="Man in Black III" length="90min"/>
<item title="Mission: Impossible" length="90min"/>
<item title="Mission: Impossible II" length="90min"/>
<item title="Mission: Impossible - The Phantom Pain" length="90min"/>
<item title="Terminator" length="90min"/>
<item title="Die Hard" length="90min"/>
<item title="Back to the Future" length="90min"/>
<item title="The Bourne Identity" length="90min"/>
<item title="Marvel's The Avengers" length="90min"/>
<item title="Jurassic Park" length="90min"/>
<item title="Jurassic World" length="90min"/>
<item title="The Dark Night" length="90min"/>
<item title="The Matrix" length="90min"/>
<item title="Kill Bill - Volume 2" length="90min"/>
<item title="Apocalypto" length="90min"/>
<item title="Apocalypse Now" length="90min"/>
<item title="Aliens" length="90min"/>

现在我想拉一个随机的电影了这些文件的。

Now I want to pull a random movie out of each of these files.

有没有一种方法可以让我直接做到这一点?还是我拉的所有项目用 XmlPullParser (如的这里),然后用随机数发生器?

Is there a way I can do that directly? Or do I have to pull all items out with XmlPullParser (like here) and then use the Random Generator?

便笺的

1:我不能完全肯定,如果我做了那些 XML 文件的方式是理想的。我只面向自己在链接的样品。如果你有更好的想法,请告诉我。

1: I'm not entirely sure if the way I made those xml files is ideal. I just oriented myself on the sample in the link. If you have better ideas, please tell me.

2:最终的目标是要在一个应用程序中显示的随机电影之后,用户选择的一个或多个流派

2: The end goal is to display a random movie in an app after the user selected one or more genres.

推荐答案

我通过与字符串数组的XML文件了另一种溃败:

I took an alternative rout by making an xml file with string arrays:

<resources>
    <string-array name="comedy">
    <item>Grown Ups</item>
    <item>Grown Ups 2</item>
    <item>Scary Movie</item>
    <item>Superbad</item>
    <item>Zoolander</item>
    <item>Groundhog Day</item>
    <item>Beverly Hills Cop</item>
    <item>Beverly Hills Cop II</item>
    <item>Hangover</item>
    <item>Anchorman</item>
    <item>Pineapple Express</item>
    <item>Happy Gilmore</item>
    <item>We are the Millers</item>
    <item>Horrible Bosses</item>
    <item>Horrible Bosses 2</item>
    <item>Meet the Parents</item>
    </string-array>

    <string-array name="action">
    <item>Man in Black</item>
    <item>Man in Black II</item>
    <item>Man in Black III</item>
    <item>Terminator</item>
    <item>Die Hard</item>
    <item>Back to the Future</item>
    <item>The Bourne Identity</item>
    <item>The Avengers</item>
    <item>Jurassic Park</item>
    <item>Jurassic World</item>
    <item>The Dark Night</item>
    <item>The Matrix</item>
    <item>Mission: Impossible</item>
    <item>Mission: Impossible II</item>
    <item>Mission: Impossible - The Phantom Pain</item>
    <item>Kill Bill - Volume 2</item>
    <item>Apocalypto</item>
    <item>Aliens</item>
    <item>Apocalypse Now</item>
    <item>Logans Run</item>
</string-array>
</resources>

现在我可以很容易地得到一个数组,然后使用随机数发生器。

Now I can easily get an array and then use the Random Generator.

到目前为止,这是不够的。

So far that's sufficient.

但后来在制作应用程序的过程中我会不仅曲风,而且还长过滤。 这就是为什么我试图用第一溃败,如在我最初的问题。 这将是巨大的,如果任何人有一个建议,使这项工作!

But later in the process of making the app I'll have to filter not only by genre, but also by length. That's why I tried to use the first rout, as shown in my initial question. It would be great, if anyone has a suggestion to make that work!