1

第一项

<item>
<title>...</title>
<link>...</link>
<guid>...</guid>
<description>...</description>
<pubDate>Thu, 31 Jan 2013 13:46:24 +0530</pubDate>
</item>

第二项

<item>
<title>...</title>
<link>...</link>
<guid>...</guid>
<description>...</description>
<enclosure url="http://entertainment.oneindia.in/img/2013/01/31-race-2-6-day-collection.jpg" type="image/jpeg" length="202500"/>
<pubDate>Thu, 31 Jan 2013 10:36:35 +0530</pubDate>
</item>

“项目”是 xml 中的元素。第一项没有“附件”标签,而在第二项中,它是可用的。我的 DOMParser 抛出 NPE,因为它正在搜索的标签“enclosure”在第一个“item”中不可用。当首选标签不可用时如何跳过?

我的 NPE 代码是

objItem.setImage(getAttributeVal("enclosure", eElement));



public String getAttributeVal(String tag,Element eElement)
    {

        String imgUrl=eElement.getElementsByTagName(tag).item(0).getAttributes().getNamedItem("url").getNodeValue();
        return imgUrl;

    }
4

3 回答 3

1

您当前的代码不允许缺少实例。它总是假设它存在。

修改您的函数以检查它是否存在。

public String getAttributeVal(String tag,Element eElement)
{
    if (eElement.GetElementsByTagName(tag).Length > 0) // Or .Count > 0
    {
       String imgUrl=eElement.getElementsByTagName(tag).item(0).getAttributes().getNamedItem("url").getNodeValue();
       return imgUrl;
    }

    return ""; // Or null
}
于 2013-01-31T10:19:08.347 回答
0

你可以使用 JaxB api:

 import java.io.File;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;



public static void main (String [] args) throws JAXBException {

File file = new File("myfile.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(MyItem.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller ();

MyItem item = (MyItem) unmarshaller.unmarshal (file);

String description = item.getDescription ();
String guid = item.getGuid ();
// ...
}


@XmlRootElement(name = "item")
@XmlAccessorType(XmlAccessType.NONE)
public static class MyItem {

public MyItem () {
  // FOR JAXB SERIALISATION
}

public String getTitle () {
  return title;
}
public void setTitle (String title) {
  this.title = title;
}
public String getLink () {
  return link;
}
public void setLink (String link) {
  this.link = link;
}
public String getGuid () {
  return guid;
}
public void setGuid (String guid) {
  this.guid = guid;
}
public String getDescription () {
  return description;
}
public void setDescription (String description) {
  this.description = description;
}
public MyEnclosre getEnclosure () {
  return enclosure;
}
public void setEnclosure (MyEnclosre enclosure) {
  this.enclosure = enclosure;
}
public String getPubDate () {
  return pubDate;
}
 public void setPubDate (String pubDate) {
  this.pubDate = pubDate;
 }

@XmlElement(name = "title")
private String title;

@XmlElement(name = "link")
private String link;

@XmlElement(name = "guid")
private String guid;

@XmlElement(name = "description")
private String description;

@XmlElement(name = "enclosure")
private MyEnclosre enclosure;

@XmlElement(name = "pubDate")
private String pubDate;

}

public static class MyEnclosre {

public MyEnclosre () {
  //  FOR JAXB SERIALISATION
}

public String getUrl () {
  return myUrl;
}

public void setUrl (String url) {
  this.myUrl = url;
}

@XmlAttribute (name = "url")
private String myUrl;

}
于 2013-01-31T13:55:08.557 回答
0

您可以尝试使用序列化,您可以为附件创建类

public class item
{
   public enclosure enclosure{get;set} // it is null if no corresponding xml presented.
}

public class enclosure 
{
   public string url {get; set;}
   public string type {get; set;}
   public string length {get; set;}
}
于 2013-01-31T09:56:35.627 回答