首页javajaxbJava HTML/XML - 如何使用JAXB Fragment Marshal w/o命名空间

Java HTML/XML - 如何使用JAXB Fragment Marshal w/o命名空间

我们想知道如何使用JAXB Fragment Marshal w/o命名空间。
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

public class Main {

  public static void main(String[] args) throws Exception {
    XMLStreamWriter writer = XMLOutputFactory.newFactory()
        .createXMLStreamWriter(System.out);
    writer.setDefaultNamespace("http://www.w3cschool.cn");

    JAXBContext jc = JAXBContext.newInstance(WorkSet.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    writer.writeStartDocument();
    writer.writeStartElement("http://www.w3cschool.cn", "Import");
    writer.writeNamespace("", "http://www.w3cschool.cn");
    writer.writeStartElement("WorkSets");

    m.marshal(new WorkSet(), writer);
    m.marshal(new WorkSet(), writer);

    writer.writeEndDocument();
    writer.close();
  }

  @XmlRootElement(name = "WorkSet", namespace = "http://www.w3cschool.cn")
  public static class WorkSet {

  }
}