xml - JAXB |带有图像 :image-Tags using "http://www.google.com/schemas/sitemap-image/1.1" 的 Google 站点地图

标签 xml image namespaces jaxb sitemap

我试图解码给定的 XML 文件,结合这些文件中的一些信息并再次编码它们,以便我可以生成一个单一的 XML 文件。但是现在我的代码有问题,因为我必须将命名空间声明从“http://www.google.com/schemas/sitemap/0.9”更改为“http://www.sitemaps.org/schemas/sitemap/0.9

在我不得不改变它之前,一切都是正确的,最后我得到了这样的 XML:

<url>
    <loc>...</loc>
    <lastmod>...</lastmod>
    <changefreq>...</changefreq>
    <priority>...</priority>
    <xhtml:link href="..." hreflang="..."/>
    <uuid>...</uuid>
    <image:image>
        <image:loc>...</image:loc>
    </image:image>
</url>

现在,在我必须更改开头提到的命名空间之后,所有 image:image- 标签都不再位于最终的 XML 中,它看起来像这样:
<url>
    <loc>...</loc>
    <lastmod>...</lastmod>
    <changefreq>...</changefreq>
    <priority>...</priority>
    <xhtml:link href="..." hreflang="..."/>
    <uuid>...</uuid>
</url>

在这里你会找到一些代码片段:

我的 package-info.java 看起来像这样:
@XmlSchema(
    namespace = "http://www.sitemaps.org/schemas/sitemap/0.9",
    elementFormDefault = XmlNsForm.QUALIFIED,
    xmlns={ @XmlNs(prefix="", 
namespaceURI="http://www.sitemaps.org/schemas/sitemap/0.9"),
            @XmlNs(prefix="image", namespaceURI="http://www.google.com/schemas/sitemap-image/1.1"),
            @XmlNs(prefix="xhtml", namespaceURI="http://www.w3.org/1999/xhtml")
   }
)
package ....sitemaptools.xml;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

我的 XMLImage 类:
package ....sitemaptools.xml;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement( name = "image")
public class XMLImage {

String loc;
public String getLoc() {return loc;}
@XmlElement( name = "loc", namespace="http://www.google.com/schemas/sitemap-image/1.1")
public void setLoc(String loc) {this.loc = loc;}

}

我的节点类的一部分:
List<XMLImage> imageList = new ArrayList<XMLImage>();
public List<XMLImage> getImage() {return imageList;}

@XmlElement(name = "image", namespace="http://www.google.com/schemas/sitemap-image/1.1")
public void setImage(List<XMLImage> images) {this.imageList = imageList;}

public void add(XMLImage image) {
    if (this.imageList == null) {
        this.imageList = new ArrayList<XMLImage>();
    }
    this.imageList.add(image);
}

如果您需要更多信息或片段,请提前告诉我并致谢!

最佳答案

借助此链接 https://www.kevinhooke.com/2014/03/25/debugging-jaxb-unmarshalling-issues/ ,我设法获取更多调试信息并发现,在我的源 XML 文件中,图像的 URL 更改为:

http://www.google.com/schemas/sitemap-image/1.1 (http)



http s ://www.google.com/schemas/sitemap-image/1.1 (https)

所以我不得不在我的代码中改变这一点,现在一切正常。

关于xml - JAXB |带有图像 :image-Tags using "http://www.google.com/schemas/sitemap-image/1.1" 的 Google 站点地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46778077/

相关文章:

html - 连续 4 张图片,如何在每张图片下居中放置特定文本?

c# - 使用 C# 解析具有多个命名空间的 Xml

使用 NameSpace 解析 XML

javascript - 如何从以下 xml 制作层次结构数组?

javascript - 使用 HTML 并排显示两个文档

c++ - 图像处理程序中的微小图像差异

c++ - 友元定义函数的命名空间是什么?

php - 如何使用 XPath 检查 <Success/> 节点是否存在

java - 使用 Java 将包含 XML 的字符串添加到 SOAPElement

java - 从 XML 保存/加载 jFreechart TimeSeriesCollection 图表