2013-01-31 2 views
1

첫번째 아이템어떻게이 XML 파일을 구문 분석 할 수 있습니까?

<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를 던졌습니다. 그 이유는 "인클로저"태그가 첫 번째 "항목"에서 사용할 수 없기 때문입니다. 선호하는 태그를 사용할 수없는 경우 어떻게 건너 뛸 수 있습니까?

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; 

    } 

답변

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

} 
관련 문제