我该如何解析这个xml文件?

时间:2013-01-31 09:52:55

标签: dom xml-parsing

第一项

<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>

“item”是xml中的元素。第一项没有“enclosure”标签,而在第二项中,它可用。我的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;

    }

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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;}
}

答案 2 :(得分: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;

}