2017-03-23 7 views
0

rometoolsでRSS XMLファイルを解析しようとしていますが、動作させることができませんでした。 Productクラスには、次のRSS XMLファイルのすべての要素が含まれています。しかし、私はエンティティにファイルを正確にマップすることができないようです。RSS XMLをrometoolsで正しく解析する方法は?

RSSのXMLファイル:

<?xml version="1.0"?> 
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"> 
<channel> 
<title>Test Store</title> 
<link>http://www.example.com</link> 
<description>An example item from the feed</description> 
<item> 
<g:id>DB_1</g:id> 
<g:title>Dog Bowl In Blue</g:title> 
<g:description>Solid plastic Dog Bowl in marine blue color</g:description> 
<g:link>http://www.example.com/bowls/db-1.html</g:link> 
<g:image_link>http://images.example.com/DB_1.png</g:image_link> 
<g:brand>Example</g:brand> 
<g:condition>new</g:condition> 
<g:availability>in stock</g:availability> 
<g:price>9.99 GBP</g:price> 
<g:shipping> 
<g:country>UK</g:country> 
<g:service>Standard</g:service> 
<g:price>4.95 GBP</g:price> 
</g:shipping> 
<g:google_product_category>Animals &gt; Pet Supplies</g:google_product_category> 
<g:custom_label_0>Made in Waterford, IE</g:custom_label_0> 
</item> 
</channel> 
</rss> 

解析方法は次のとおりです。

Product product = new Product(); 

try { 
    SyndFeedInput input = new SyndFeedInput(); 

    SyndFeed feed = input.build(new XmlReader(file)); 


    Namespace ns = Namespace.getNamespace("g", "http://base.google.com/ns/1.0"); 

    for (SyndEntry entry : feed.getEntries()) { 
     for (Element element : entry.getForeignMarkup()) { 
      product.setId(element.getAttribute("id", ns).getValue()); // **id 
     } 
    } 

    System.out.println(product.getId()); 
} catch (Exception e) { 
    ex.printStackTrace(); 
} 

一部// **idコメントでNullPointerExceptionが返されます。

ここで何が間違っていますか?これをどうやって稼働させるか?

答えて

0

Namespaceの部分は必要ありません。これは次のようなものです:

for (SyndEntry entry : feed.getEntries()) { 
    for (Element element : entry.getForeignMarkup()) { 
     if (element.getQualifiedName().equalsIgnoreCase("g:id")) { 
       product.setId(element.getValue()); 
     } 
    } 
} 
関連する問題