XML アナウンスを解析するためにあなたの助けが必要です。
以下の XML を見てみましょう。
<?xml version="1.0" encoding="UTF-8"?>
<FAVORIS>
<LOGGED>1</LOGGED>
<NOTICES NUM_PAGE="2">
<NOTICE>
<DEPARTMENT>33</DEPARTMENT>
<SOURCE>EMP</SOURCE>
<OBJET><![CDATA[Fourniture d'une solution régionale]]></OBJET>
<ID_PROCEDURE>7543</ID_PROCEDURE>
<ORGANISME><![CDATA[Gcs]]></ORGANISME>
<TYPE_AVIS>ZZ</TYPE_AVIS>
<TYPE_PROCEDURE>W3</TYPE_PROCEDURE>
<CATEGORIE>Service</CATEGORIE>
<DATE_OFFRE>16/09/2013 12:00</DATE_OFFRE>
<DECALAGE_HORAIRE>0</DECALAGE_HORAIRE>
</NOTICE>
<NOTICE>
<DEPARTMENT>33</DEPARTMENT>
<SOURCE>EMP</SOURCE>
<OBJET><![CDATA[Refonte du portail]]></OBJET>
<ID_PROCEDURE>4323</ID_PROCEDURE>
<ORGANISME><![CDATA[Mairie de W]]></ORGANISME>
<TYPE_AVIS>Z</TYPE_AVIS>
<TYPE_PROCEDURE>W1</TYPE_PROCEDURE>
<CATEGORIE>Service</CATEGORIE>
<DATE_OFFRE>03/09/2013 12:00</DATE_OFFRE>
<DECALAGE_HORAIRE>0</DECALAGE_HORAIRE>
</NOTICE>
....
</NOTICES>
</FAVORIS>
この XML を解析するために、http: //developer.android.com/training/basics/network-ops/xml.html の XML 解析に関するチュートリアルに従いました。
この XML を解析すると、次のようなエラーが発生します。
expected: START_TAG {null}NOTICES (position:START_TAG <FAVORIS>@2:10 in java.io.InputStreamReader@40e69360)
これは require によるものですが、どうすればこの問題を解決できますか?
私のコードの詳細:
public class AOThumbnail_ParserXML {
private static final String ns = null;
public ArrayList<AOThumbnail> parse(String in) throws XmlPullParserException, IOException {
Decoder decoder = new Decoder();
String decoded_in = decoder.decode(in);
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new ByteArrayInputStream(decoded_in.getBytes()), null);
parser.nextTag();
return readFeed(parser);
}
private ArrayList<AOThumbnail> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
ArrayList<AOThumbnail> entries = new ArrayList<AOThumbnail>();
parser.require(XmlPullParser.START_TAG, ns, "NOTICES");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("NOTICE")) {
entries.add(readEntry(parser));
} else {
skip(parser);
}
}
return entries;
}
private AOThumbnail readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "NOTICE");
String organisme = null;
String objet = null;
String date_candidature = null;
String date_offre = null;
String departement = null;
String source = null;
String idProcedure = null;
String decalageHoraire = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("ORGANISME")) {
organisme = read(parser, "ORGANISME");
} else if (name.equals("OBJET")) {
objet = read(parser, "OBJET");
} else if (name.equals("DATE_CAND")) {
date_candidature = read(parser, "DATE_CAND");
} else if (name.equals("DATE_OFFRE")) {
date_offre = read(parser, "DATE_OFFRE");
} else if (name.equals("DEPARTMENT")) {
departement = read(parser, "DEPARTMENT");
} else if (name.equals("SOURCE")) {
source = read(parser, "SOURCE");
} else if (name.equals("ID_PROCEDURE")) {
idProcedure = read(parser, "ID_PROCEDURE");
} else if (name.equals("DECALAGE_HORAIRE")) {
decalageHoraire = read(parser, "DECALAGE_HORAIRE");
} else {
skip(parser);
}
}
String date = "";
try {
if (date_candidature.length() > 0)
date = date_candidature;
else if (date_offre.length() > 0)
date = date_offre;
else
date = "Se référer à l'annonce";
} catch (Exception ex) {
Logger.logit(ex);
}
return new AOThumbnail(organisme, objet, date, departement, idProcedure, source, Boolean.parseBoolean(decalageHoraire));
}
private String read(XmlPullParser parser, String balise) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, balise);
String content = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, balise);
return content;
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
}
このような XML を解析すると (以下を参照)、動作しますが、上記の XML では動作しません!
<NOTICES NUM_PAGE="2">
<NOTICE>
<DEPARTMENT>33</DEPARTMENT>
<SOURCE>EMP</SOURCE>
<OBJET><![CDATA[Fourniture d'une solution régionale]]></OBJET>
<ID_PROCEDURE>7543</ID_PROCEDURE>
<ORGANISME><![CDATA[Gcs]]></ORGANISME>
<TYPE_AVIS>ZZ</TYPE_AVIS>
<TYPE_PROCEDURE>W3</TYPE_PROCEDURE>
<CATEGORIE>Service</CATEGORIE>
<DATE_OFFRE>16/09/2013 12:00</DATE_OFFRE>
<DECALAGE_HORAIRE>0</DECALAGE_HORAIRE>
</NOTICE>
<NOTICE>
<DEPARTMENT>33</DEPARTMENT>
<SOURCE>EMP</SOURCE>
<OBJET><![CDATA[Refonte du portail]]></OBJET>
<ID_PROCEDURE>4323</ID_PROCEDURE>
<ORGANISME><![CDATA[Mairie de W]]></ORGANISME>
<TYPE_AVIS>Z</TYPE_AVIS>
<TYPE_PROCEDURE>W1</TYPE_PROCEDURE>
<CATEGORIE>Service</CATEGORIE>
<DATE_OFFRE>03/09/2013 12:00</DATE_OFFRE>
<DECALAGE_HORAIRE>0</DECALAGE_HORAIRE>
</NOTICE>
....
</NOTICES>
あなたが私を助けてくれることを願っています、どうもありがとう!