[英]Merge multiple XML files with Groovy
我对Groovy还是很陌生,实际上是昨天才发现的。 我正在使用MkDocs静态网站生成器构建网站,而新的内部要求导致我不得不将当前网站分为三个不同的网站容器,以确保唯一的搜索索引,依此类推。 这一切都由詹金斯(Jenkins)处理和构建,并且运行良好。
不幸的是,此解决方案导致我需要合并三个不同的站点地图,并建议研究Groovy。 我从Groovy中获得了以下大部分代码-合并XML节点 ,除了将第一个站点地图写入文件中之外,没有得到任何其他结果。 有什么建议可以解决这里的问题吗?
//Define XML objects to parse, set namespace to false.
def sm1 = new XmlSlurper( false, false ).parse(new File('C://test/site-1/sitemap.xml'))
def sm2 = new XmlSlurper( false, false ).parse(new File('C://test/site-2/sitemap.xml'))
def sm3 = new XmlSlurper( false, false ).parse(new File('C://test/site-3/sitemap.xml'))
//Define the output file.
def output = new File ('C://test/sitemap.xml')
//Append url-nodes from sitemap 2 to sitemap 1 urlset.
sm2.'**'.findAll{it.name() == 'url'}.collect{ sm1.urlset.appendNode(it)}
//Append url-nodes from sitemap 3 to sitemap 1 urlset.
sm3.'**'.findAll{it.name() == 'url'}.collect{ sm1.urlset.appendNode(it)}
//Define what to write to file.
def content = groovy.xml.XmlUtil.serialize(sm1)
//Write to file.
output.newWriter().withWriter { w ->
w << content
}
/site-1/sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://site/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>
/site-2/sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://site/site-2/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://site/site-2/section/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>
/site-3/sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://site/site-3/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://site/site-3/section/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>
预期产量
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://site/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://site/site-2/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://site/site-2/section/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://site/site-3/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://site/site-3/section/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>
电流输出
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://site/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>
1.在xml解析变量已经引用根元素之后,因此要访问urlset
标签,您只需要使用sm1...
而不是sm1.urlset...
collect
可以工作,但是在这种情况下最好使用它们 下面的工作代码:
def sm1 = new XmlSlurper( false, false ).parseText('''<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://site/site-1/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://site/site-1/section/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>''')
def sm2 = new XmlSlurper( false, false ).parseText('''<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://site/site-2/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://site/site-2/section/</loc>
<lastmod>2019-01-18</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>''')
//Append url-nodes from sitemap 2 to sitemap 1 urlset.
println sm2.url.each{println sm1.appendNode(it)}
//Define what to write to file.
def content = groovy.xml.XmlUtil.serialize(sm1)
println content
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.