Camel 4.0.4升级后SAXParseException报错求助
问题描述
将Camel版本升级至4.0.4并切换到Java 17后,加载camel-spring.xsd创建Schema时触发org.xml.sax.SAXParseException错误,提示circuitBreakerDefinition元素下存在两个onFallback元素,违反Unique Particle Attribution规则。手动注释其中一个onFallback元素可消除错误,但无法修改Jar包中的xsd文件,急需获取可用Camel 4.x版本Schema的解决方法。
报错堆栈
org.xml.sax.SAXParseException; lineNumber: 4268; columnNumber: 51; cos-nonambig: "http://camel.apache.org/schema/spring":onFallback and "http://camel.apache.org/schema/spring":onFallback (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles. at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source) at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) at org.apache.xerces.impl.xs.XSConstraints.reportSchemaError(Unknown Source) at org.apache.xerces.impl.xs.XSConstraints.fullSchemaChecking(Unknown Source) at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source) at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source) at org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
测试代码片段
import com.google.common.io.Resources; import javax.xml.XMLConstants; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.SchemaFactory; import java.io.ByteArrayInputStream; import java.net.URL; import java.util.ArrayList; import java.util.List; public class Camel { public static void main( String[] args ) throws Exception { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); List<Source> tmp = new ArrayList(); String loc = "camel-spring.xsd"; URL url = Resources.getResource(loc); byte[] bytes = Resources.toByteArray(url); StreamSource src = new StreamSource(new ByteArrayInputStream(bytes)); tmp.add(src); schemaFactory.newSchema((Source[]) tmp.toArray(new Source[tmp.size()])); } }
解决方法
- 升级到Camel 4.x修复版本:该Schema重复定义问题是Camel 4.0.4的已知bug,后续版本(如4.0.5及以上)已修复xsd中的重复元素问题,直接升级Camel版本即可获取正确的Schema文件。
- 临时动态修改xsd内容:若暂时无法升级版本,可在加载xsd时动态修改字节流,移除重复的
onFallback元素。示例代码修改如下:
// 读取xsd内容转为字符串 String xsdContent = new String(bytes); // 移除重复的onFallback元素(需根据实际xsd内容调整匹配规则) xsdContent = xsdContent.replaceAll("<xs:element name=\"onFallback\" type=\"xs:string\" minOccurs=\"0\" maxOccurs=\"1\"/>", ""); // 重新转为输入流 StreamSource src = new StreamSource(new ByteArrayInputStream(xsdContent.getBytes()));
注意:需确保正则匹配的是重复的元素,避免误删正常定义。
- 切换SchemaFactory实现(不推荐):部分SchemaFactory支持忽略特定错误,但Xerces默认不支持。尝试切换到其他实现(如Apache Santuario)可能绕过错误,但会引入兼容性风险,仅作为临时应急方案。
内容的提问来源于stack exchange,提问作者Rebeca Baptista
相关产品推荐
相关产品推荐

