如何在groovy中读取XML中的多行

时间:2021-07-20 01:36:23
<?xml version="1.0" encoding="ISO-8859-1" ?> 
    <!--  generator=Moneycontrol XML FEED Generator  --> 
 <rss version="2.0">
    <channel>
        <title>Moneycontrol Business News</title>
        <description>Business News from Moneycontrol.com</description>
        <link>http://www.moneycontrol.com</link>
        <lastBuildDate>Tue, 07 Aug 2012 10:50:03 +0530</lastBuildDate>
        <generator>Moneycontrol.com</generator>
        <image>
            <url>http://img1.moneycontrol.com/images/top2010/moneycontrol_logo.jpg</url>
            <title>Moneycontrol Logo</title>
            <link>http://www.moneycontrol.com</link>
        <description>Feed provided by Moneycontrol.</description>
        </image>
        <item>
        <title>Expect prices to range between Rs 35-40/kg: Sakthi Sugars</title>
        <link>http://www.moneycontrol.com/news/business/expect-prices-to-range-between-rs-35-40kg-sakthi-sugars_741216.html</link>
        <description>&lt;img src=&quot;http://www.moneycontrol.com/news_image_files/2012/m/M-Manickam-aug7-190.jpg&quot; alt=&quot;M Manickam&quot; title=&quot;M Manickam&quot; border=&quot;0&quot; width=&quot;75&quot; height=&quot;75&quot; align=&quot; left&quot; hspace=&quot;5&quot;/&gt; M Manickam, managing director of Sakthi Sugars says, he expects the sugar prices to range between Rs 35-40 per kilogram.</description>
        <pubDate>Tue, 07 Aug 2012 10:32:01 +0530</pubDate>
        <guid>http://www.moneycontrol.com/news/business/expect-prices-to-range-between-rs-35-40kg-sakthi-sugars_741216.html</guid>
        </item>
        <item>
        <title>Maruti factory riot sounds alarm bells for industry</title>
        <link>http://www.moneycontrol.com/news/business/maruti-factory-riot-sounds-alarm-bells-for-industry_741130.html</link>
        <description>&lt;img src=&quot;http://www.moneycontrol.com/news_image_files/2012/m/maruti_unrest.jpg&quot; alt=&quot;Maruti factory riot sounds alarm bells for industry&quot; title=&quot;Maruti factory riot sounds alarm bells for industry&quot; border=&quot;0&quot; width=&quot;75&quot; height=&quot;75&quot; align=&quot; left&quot; hspace=&quot;5&quot;/&gt; Hiding in his office near New Delhi as workers armed with iron bars and car parts rampaged through the factory, Maruti Suzuki supervisor Raj Kumar spent two terrified hours trying to comprehend the warzone his workplace had become.</description>
        <pubDate>Tue, 07 Aug 2012 08:02:02 +0530</pubDate>
        <guid>http://www.moneycontrol.com/news/business/maruti-factory-riot-sounds-alarm-bells-for-industry_741130.html</guid>
        </item>
        <item>
        <title>New base price will push call rates up 49 p: COAI</title>
        <link>http://www.moneycontrol.com/news/business/new-base-price-will-push-call-rates49-p-coai_741067.html</link>
        <description>&lt;img src=&quot;http://www.moneycontrol.com/news_image_files/Telecom_new2_190.jpg&quot; alt=&quot;New base price will push call rates up 49 p: COAI&quot; title=&quot;New base price will push call rates up 49 p: COAI&quot; border=&quot;0&quot; width=&quot;75&quot; height=&quot;75&quot; align=&quot; left&quot; hspace=&quot;5&quot;/&gt; Telecom industry body COAI today claimed that the Rs 14,000 crore base price fixed by Cabinet for auction of telecom spectrum will lead to 37-49 paise per minute increase in call tariffs.</description>
        <pubDate>Mon, 06 Aug 2012 22:49:35 +0530</pubDate>
        <guid>http://www.moneycontrol.com/news/business/new-base-price-will-push-call-rates49-p-coai_741067.html</guid>
        </item>
    </channel>
 </rss>

I wanted to read the multiple rows in the xml file got from a partuclar link which contains multiple tags, wanted to loop through the whole xml data and fetch each records and insert into database.

我想读取xml文件中的多行,这些行来自包含多个标记的partuclar链接,想要遍历整个xml数据并获取每个记录并插入到数据库中。

1 个解决方案

#1


1  

You can do this (as an example) to print the title of each item:

您可以这样做(作为示例)打印每个项目的标题:

 new XmlParser().parseText( xml ).channel.item.each {
   println it.title.text()
 }

If you have the xml as a String. If it is not a String, you'd want to use one of the 3 forms of parse instead of parseText

如果您将xml作为String。如果它不是String,您需要使用3种形式的解析而不是parseText

#1


1  

You can do this (as an example) to print the title of each item:

您可以这样做(作为示例)打印每个项目的标题:

 new XmlParser().parseText( xml ).channel.item.each {
   println it.title.text()
 }

If you have the xml as a String. If it is not a String, you'd want to use one of the 3 forms of parse instead of parseText

如果您将xml作为String。如果它不是String,您需要使用3种形式的解析而不是parseText