0

Android で XML を解析するのに問題があります。私は次のXMLを持っています

<iq xmlns="jabber:client" type="result" to="blob@faisal-system/68bb97e7">
  <album xmlns="naseebalbum">
    <albumpicture>
      <title>day1</title>
      <creationdate>1397502000000</creationdate>
      <picture>BASE64EncodedStringOfImage</picture>
    </albumpicture>
    <comments>
      <comment>
        <commentid>1</commentid>
        <username>sana</username>
        <text>i loved that pic</text>
        <commenttime>1398264140000</commenttime>
      </comment>
    </comments>
    <likes>
      <like>
        <likeid>4</likeid>
        <username>sana</username>
        <liketime>1398250919000</liketime>
      </like>
    </likes>
  </album>
</iq>

誰でもこれで私を助けることができますか?

Likesタグコメントタグtitleタグとpictureタグからデータを取得したいです。

これが私がやろうとしてきたことです。

public IQ parseIQ(XmlPullParser parser) throws Exception {
        // TODO Auto-generated method stub
        payload=""+parser.getText();

         StringBuilder sb = new StringBuilder();
            int depth = 1;
            while (depth != 0) {

                switch (parser.next()) {
                case XmlPullParser.END_TAG:
                    depth--;
                    if (depth > 0) {
                        sb.append("</" + parser.getName() + ">");
                    }
                    break;
                case XmlPullParser.START_TAG:
                    depth++;
                    StringBuilder attrs = new StringBuilder();
                    for (int i = 0; i < parser.getAttributeCount(); i++) {
                        attrs.append(parser.getAttributeName(i) + "=\""
                                + parser.getAttributeValue(i) + "\" ");
                    }
                    sb.append("<" + parser.getName() + " " + attrs.toString() + ">");

                    break;
                default:
                    sb.append(parser.getText());
                    break;
                }
            }
            payload = sb.toString();

        iq=new CustomIQ(payload);
        iq.setType(Type.RESULT);

        return iq;
    }
4

4 に答える 4

0

https://github.com/Cruisoring/EasyXMLは、たとえば、XML を解析してマップにする手段を提供します。

@Test
public void testDocument_mapOf() {

    URL url = Thread.currentThread().getContextClassLoader()
        .getResource("books.xml");

    Document doc = EasySAXParser.parse(url);

    List<? extends Map<String, String>> maps = doc.mapOf("book");

    System.out.println(maps.get(0));

    System.out.println(maps.get(1));
}

結果は、以下に示す最初の 2 つの XML 要素の 12 本の 12 Map です。

   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
<!--       <price>44.95</price> -->
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>

対応する最初の 2 つの Map が出力されます。

{author=Gambardella, Matthew, genre=Computer, description=An in-depth look at creating applications 
      with XML., id=bk101, title=XML Developer's Guide, publish_date=2000-10-01}
{author=Ralls, Kim, price=5.95, genre=Fantasy, description=A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen of the world., id=bk102, title=Midnight Rain, publish_date=2000-12-16}
于 2015-06-12T04:29:23.747 に答える