I'm using the JAXB_FRAGMENT property for my marshaller to marshal at the WorkSet level. The problem is that when I marshal it's giving the WorkSet element the xmlns attribute everytime. Is there a way to marshal so that it doesn't attach the xmlns attribute? Here's what my XML looks like.
我正在使用JAXB_FRAGMENT属性为我的marshaller编组到工作集级别。问题是,当我整理它时,它会给WorkSet元素每次都提供xmlns属性。是否有一种方法可以让它不附加xmlns属性?下面是我的XML的样子。
<Import>
<WorkSets>
<WorkSet xmlns="http://www.namespace.com">
<Work>
<Work>
...
..
...
</WorkSet>
<WorkSet xmlns="http://www.namespace.com">
<Work>
<Work>
...
</WorkSet>
</WorkSets>
</Import>
Here's the code I'm using the create the above:
下面是我使用的代码创建的代码:
FileOutputStream fos = new FileOutputStream("import.xml");
XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(fos);
JAXBContext jc = JAXBContext.newInstance(WorkSet.class);
Marshaller m = jc.createMarshaler();
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
writer.writeStartDocument();
writer.writeStartElement("Import");
writer.writeAttribute("xmlns","http://www.namespace.com");
writer.writeStartElement("WorkSets");
while(hasWorkSet){
m.marshal(workSet, writer)
}
writer.writeEndDocument();
writer.close();
3 个解决方案
#1
8
Assuming you want the default namespace for your document to be http://www.namespace.com
, you could do the following:
假设您希望文档的默认名称空间是http://www.namespace.com,您可以执行以下操作:
Demo
演示
The XMLStreamWriter.setDefaultNamespace(String)
and XMLStreamWriter.writeNamespace(String, String)
methods will be used to set and write the default namespace for the XML document.
和XMLStreamWriter XMLStreamWriter.setDefaultNamespace(字符串)和的。writeNamespace(String, String)方法将用于为XML文档设置和写入默认的名称空间。
package forum9297872;
import javax.xml.bind.*;
import javax.xml.stream.*;
public class Demo {
public static void main(String[] args) throws Exception {
XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out);
writer.setDefaultNamespace("http://www.namespace.com");
JAXBContext jc = JAXBContext.newInstance(WorkSet.class);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
writer.writeStartDocument();
writer.writeStartElement("http://www.namespace.com", "Import");
writer.writeNamespace("", "http://www.namespace.com");
writer.writeStartElement("WorkSets");
m.marshal(new WorkSet(), writer);
m.marshal(new WorkSet(), writer);
writer.writeEndDocument();
writer.close();
}
}
WorkSet
WorkSet
My assumption is that you have specified namespace information in your JAXB model.
我的假设是您在JAXB模型中指定了名称空间信息。
package forum9297872;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="WorkSet", namespace="http://www.namespace.com")
public class WorkSet {
}
Output
输出
Below is the output from running the demo code:
下面是运行演示代码的输出:
<?xml version="1.0" ?><Import xmlns="http://www.namespace.com"><WorkSets><WorkSet></WorkSet><WorkSet></WorkSet></WorkSets></Import>
#2
1
There are three workarounds for this.
这有三个变通方法。
1) Create JAXB annotated objects for the container of your workersets. Add the workersets to that object and then marshal the whole thing.
1)为您的工作集的容器创建JAXB注释对象。将workerset添加到该对象中,然后将整个操作集合起来。
2) Follow the first example in 101 ways to marshal objects with JAXB and use DocumentBuilderFactory
with namespace aware.
2)遵循第一个示例,以101种方法将对象与JAXB结合,并使用具有名称空间的DocumentBuilderFactory。
3) Assuming that the jaxb object is in a package that should never have qualified namespaces you can add the following to the package annotation: (note: it's been a while since i've done this and I havn't tested this code)
假设jaxb对象在一个包中,该包不应该有限定的名称空间,您可以将以下内容添加到包注释中:(注意:我已经做了一段时间了,我还没有测试过这个代码)
@XmlSchema(namespace = "", elementFormDefault = XmlNsForm.UNQUALIFIED)
package example;
#3
0
Just another example:
另一个例子:
try {
JAXBContext customerInformationRequestContext = JAXBContext.newInstance(CustomerInformationRequest.class);
Unmarshaller unmarshaller = customerInformationRequestContext.createUnmarshaller();
StringReader stringReader = new StringReader(requestPayload);
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(stringReader);
XMLStreamReaderWrapper reader = new XMLStreamReaderWrapper(xsr);
CustomerInformationRequest customerInformationRequest = (CustomerInformationRequest) unmarshaller.unmarshal(reader);
CustomerInformationResponse customerInformationResponse = customerLookup(customerInformationRequest, sessionTransaction);
JAXBContext customerInformationResponseContext = JAXBContext.newInstance(CustomerInformationResponse.class);
Marshaller marshaller = customerInformationResponseContext.createMarshaller();
StringWriter stringWriter = new StringWriter();
XMLOutputFactory xof = XMLOutputFactory.newFactory();
XMLStreamWriter xsw = xof.createXMLStreamWriter(stringWriter);
xsw = new XMLStreamWriterWrapper(xsw);
marshaller.marshal(customerInformationResponse, xsw);
String responsePayload = stringWriter.toString();
return responsePayload;
} catch (Exception e) {
log.debug("The payload could not be unmarshalled.", e);
return null;
}
#1
8
Assuming you want the default namespace for your document to be http://www.namespace.com
, you could do the following:
假设您希望文档的默认名称空间是http://www.namespace.com,您可以执行以下操作:
Demo
演示
The XMLStreamWriter.setDefaultNamespace(String)
and XMLStreamWriter.writeNamespace(String, String)
methods will be used to set and write the default namespace for the XML document.
和XMLStreamWriter XMLStreamWriter.setDefaultNamespace(字符串)和的。writeNamespace(String, String)方法将用于为XML文档设置和写入默认的名称空间。
package forum9297872;
import javax.xml.bind.*;
import javax.xml.stream.*;
public class Demo {
public static void main(String[] args) throws Exception {
XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out);
writer.setDefaultNamespace("http://www.namespace.com");
JAXBContext jc = JAXBContext.newInstance(WorkSet.class);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
writer.writeStartDocument();
writer.writeStartElement("http://www.namespace.com", "Import");
writer.writeNamespace("", "http://www.namespace.com");
writer.writeStartElement("WorkSets");
m.marshal(new WorkSet(), writer);
m.marshal(new WorkSet(), writer);
writer.writeEndDocument();
writer.close();
}
}
WorkSet
WorkSet
My assumption is that you have specified namespace information in your JAXB model.
我的假设是您在JAXB模型中指定了名称空间信息。
package forum9297872;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="WorkSet", namespace="http://www.namespace.com")
public class WorkSet {
}
Output
输出
Below is the output from running the demo code:
下面是运行演示代码的输出:
<?xml version="1.0" ?><Import xmlns="http://www.namespace.com"><WorkSets><WorkSet></WorkSet><WorkSet></WorkSet></WorkSets></Import>
#2
1
There are three workarounds for this.
这有三个变通方法。
1) Create JAXB annotated objects for the container of your workersets. Add the workersets to that object and then marshal the whole thing.
1)为您的工作集的容器创建JAXB注释对象。将workerset添加到该对象中,然后将整个操作集合起来。
2) Follow the first example in 101 ways to marshal objects with JAXB and use DocumentBuilderFactory
with namespace aware.
2)遵循第一个示例,以101种方法将对象与JAXB结合,并使用具有名称空间的DocumentBuilderFactory。
3) Assuming that the jaxb object is in a package that should never have qualified namespaces you can add the following to the package annotation: (note: it's been a while since i've done this and I havn't tested this code)
假设jaxb对象在一个包中,该包不应该有限定的名称空间,您可以将以下内容添加到包注释中:(注意:我已经做了一段时间了,我还没有测试过这个代码)
@XmlSchema(namespace = "", elementFormDefault = XmlNsForm.UNQUALIFIED)
package example;
#3
0
Just another example:
另一个例子:
try {
JAXBContext customerInformationRequestContext = JAXBContext.newInstance(CustomerInformationRequest.class);
Unmarshaller unmarshaller = customerInformationRequestContext.createUnmarshaller();
StringReader stringReader = new StringReader(requestPayload);
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(stringReader);
XMLStreamReaderWrapper reader = new XMLStreamReaderWrapper(xsr);
CustomerInformationRequest customerInformationRequest = (CustomerInformationRequest) unmarshaller.unmarshal(reader);
CustomerInformationResponse customerInformationResponse = customerLookup(customerInformationRequest, sessionTransaction);
JAXBContext customerInformationResponseContext = JAXBContext.newInstance(CustomerInformationResponse.class);
Marshaller marshaller = customerInformationResponseContext.createMarshaller();
StringWriter stringWriter = new StringWriter();
XMLOutputFactory xof = XMLOutputFactory.newFactory();
XMLStreamWriter xsw = xof.createXMLStreamWriter(stringWriter);
xsw = new XMLStreamWriterWrapper(xsw);
marshaller.marshal(customerInformationResponse, xsw);
String responsePayload = stringWriter.toString();
return responsePayload;
} catch (Exception e) {
log.debug("The payload could not be unmarshalled.", e);
return null;
}