gpt4 book ai didi

xml - 我如何摆脱 xml :base attribute that is added to my xml document after using xinclude?

转载 作者:数据小太阳 更新时间:2023-10-29 02:26:53 24 4
gpt4 key购买 nike

我正在尝试使用 xinclude 将 xml 文件解码为 java 对象。我有一个基于我的 jaxb 注释代码文件的模式。这是我的解码代码:

@Override
public T readFromReader(final Reader reader) throws Exception {
final Unmarshaller unmarshaller = createUnmarshaller();

final SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setXIncludeAware(true);
spf.setNamespaceAware(true);
//spf.setValidating(true);

final XMLReader xr = spf.newSAXParser().getXMLReader();
final InputSource inputSource = new InputSource(reader);
xr.parse(inputSource)

final SAXSource source = new SAXSource( xr, new InputSource(reader) );


try {
final T object = (T) unmarshaller.unmarshal(source);
postReadSetup(object);
return object;
} catch (final Exception e) {
throw new RuntimeException("Cannot parse XML: Additional information is attached. Please ensure your XML is valid.", e);
}
}

由于我正在调用 xr.parse(),实际的解码没有成功(因为解析关闭了流)。但是,我收到一个解析错误 [Error] file-name.xml:2:66: cvc-complex-type.3.2.2: Attribute 'xml:base' is not allowed to appear in element 'element name '.

我发现这是因为 xinclude 总是向包含的元素添加 xml:base 属性以帮助保留基本 uri。问题是我的模式不允许这个属性。我有什么办法摆脱这个吗?我在网上四处查看,并没有真正看到任何可以轻松实现的东西。

最佳答案

我添加了以下代码行:

// to get rid of xml:base that is brought in with xinclude
spf.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);

在我根据 this article 将解析器工厂设置为 xinclude 感知的同一位置

关于xml - 我如何摆脱 xml :base attribute that is added to my xml document after using xinclude?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17352597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com