我正在使用SAX Parser来处理我正在处理的Android应用程序,并且我很难尝试保存Project的名称标签而不被覆盖。这里的问题是项目中的名称标签被许可证中的名称标签覆盖,因为标签是相同的。剩下的就是解析得很漂亮。此外,通过将GET请求传递给API来生成此xml,因此我无法对其进行编辑。有没有办法可以省略许可证字段中的名称标签,或者尝试了解我在文档内部的位置,这是一个问题,因为SAX甚至被驱动。此外,我意识到我可以使用DOM轻松完成这项工作,但现在我已经走得太远而无法切换到DOM。我已经附加了我的XML处理代码和我试图解析的xml文件的片段。
由于
@Override
public void startElement (String namespaceURI, String localName, String qName, Attributes attributes) throws SAXException
{
buffer.setLength(0);
if (localName.equalsIgnoreCase("result"))
{
results = new ArrayList <Result> ();
}
if (localName.equalsIgnoreCase("project"))
{
result = new Result ();
}
if (localName.equalsIgnoreCase("name"))
{
result.name = buffer.toString ();
Log.v("PROJECT NAME: ", result.name);
}
} // end of startElement ()
@Override
public void endDocument () throws SAXException
{}
@Override
public void endElement (String namespaceURI, String localName, String qName) throws SAXException
{
if (localName.equalsIgnoreCase("project") || localName.equalsIgnoreCase("account"))
{
this.entries++;
results.add(result);
}
else if (localName.equalsIgnoreCase("id"))
{
result.id = buffer.toString ();
Log.v("Id: ", result.id);
}
else if (localName.equalsIgnoreCase("created_at"))
{
try
{
result.date_created = ResultHandlerProject.DATE_FORMAT.parse(buffer.toString());
}
catch (ParseException e)
{
e.printStackTrace();
}
}
else if (localName.equalsIgnoreCase("updated_at"))
{
try
{
result.date_updated = ResultHandlerProject.DATE_FORMAT.parse(buffer.toString());
}
catch (ParseException e)
{
e.printStackTrace();
}
}
else if (localName.equalsIgnoreCase("description"))
{
result.description = buffer.toString ();
}
else if (localName.equalsIgnoreCase("homepage_url"))
{
result.url_homepage = buffer.toString ();
}
else if (localName.equalsIgnoreCase("download_url"))
{
result.url_download = buffer.toString ();
}
else if (localName.equalsIgnoreCase("medium_logo_url"))
{
result.image_link = buffer.toString ();
}
else if (localName.equalsIgnoreCase("user_count"))
{
result.user_count = buffer.toString ();
}
else if (localName.equalsIgnoreCase("average_rating"))
{
result.rating = buffer.toString ();
}
else if (localName.equalsIgnoreCase("rating_count"))
{
result.rating_count = buffer.toString ();
}
else if (localName.equalsIgnoreCase("analysis_id"))
{
result.analysis_id = buffer.toString ();
}
else if (localName.equalsIgnoreCase("nice_name"))
{
result.license_full_name = buffer.toString ();
Log.v("License Full Name: ", result.license_full_name);
}
} // end of endElement ()
示例XML文件:
<result>
<project>
<id>6050</id>
<name>FirePHP</name>
<created_at>2007-06-16T04:03:13Z</created_at>
<updated_at>2012-07-01T15:05:23Z</updated_at>
<description>
FirePHP
<homepage_url>http://www.firephp.org/</homepage_url>
<download_url>http://www.firephp.org/</download_url>
<url_name>FirePHP</url_name>
<medium_logo_url>
https://s3.amazonaws.com/cloud.ohloh.net/attachments/6638/FirePHP_Large_White_med.png
</medium_logo_url>
<small_logo_url>
https://s3.amazonaws.com/cloud.ohloh.net/attachments/6638/FirePHP_Large_White_small.png
</small_logo_url>
<user_count>145</user_count>
<average_rating>4.11765</average_rating>
<rating_count>34</rating_count>
<analysis_id>8910317</analysis_id>
<licenses>
<license>
<name>bsd</name>
<nice_name>BSD Copyright</nice_name>
</license>
</licenses>
</project>
答案 0 :(得分:0)
您可以通过简单的方式解决问题。
在您的类处理程序中创建一些标志。
private boolean inProject = false;
private boolean inLicense = false;
控制这些标志,就像我在下面给你看的那样。
@Override
public void startElement (String namespaceURI, String localName, String qName, Attributes attributes) throws SAXException
{
if (localName.equalsIgnoreCase("project")) {
this.inProject = true;
}
else if (localName.equalsIgnoreCase("license")) {
this.inLicense = true;
}
}
@Override
public void endElement (String namespaceURI, String localName, String qName) throws SAXException
{
if (localName.equalsIgnoreCase("name") {
if(inProject) {
//Set your project name here.
this.inProject = false;
}
else if (inLicense) {
//Set your lencense name here.
this.inLicense = false;
}
}
}
让我知道它是否有效。