去除xmlns:xsi=""时报错: jaxb.noNamespaceSchemaLocation must be a String
时间: 2024-03-15 15:46:14 浏览: 11
这个错误是由于在 XML 文件中使用了 `xmlns:xsi=""` 命名空间,但是没有设置 `jaxb:noNamespaceSchemaLocation` 属性值导致的。可以尝试在 XML 文件中添加以下属性来解决这个问题:
```
<rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="schema.xsd">
...
</rootElement>
```
其中,`schema.xsd` 是指向 XML Schema 文件的路径。这样就可以正确地去除 `xmlns:xsi=""` 命名空间,并且设置了正确的 Schema 文件。
相关问题
Jaxb bean转化为xml格式时,去除xsi:type="" xmlns:xsi=""
在JAXB中,如果不想在XML中包含xsi:type和xmlns:xsi属性,可以通过在生成的JAXBContext中设置Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION属性来实现。具体地说,可以按照以下步骤操作:
1. 创建JAXBContext对象时,使用JAXBContext.newInstance(Class... contextPath, Map<String,?> properties)方法,并将Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION属性设置为true。
例如:
```
JAXBContext context = JAXBContext.newInstance(Person.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, true);
```
2. 将JAXB对象转换为XML时,将Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION属性设置为true。
例如:
```
Person person = new Person();
// 设置person的属性
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, true);
marshaller.marshal(person, outputStream);
```
这样,在将JAXB对象转换为XML时,就不会包含xsi:type和xmlns:xsi属性了。
<?xml version="1.0" encoding="utf-16"?> <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <telegramType value="Store_Out" /> <ID value="1" /> </Root> 上列中 root 里的默认数据xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"如何去掉
要去掉默认数据xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema",可以将Root节点的属性改为以下形式:
```
<Root telegramType="Store_Out" ID="1">
```
这样就可以去掉默认数据了。