需要使用JAXB STAX结合,获取没有结束标签的XML根元素。

huangapple go评论80阅读模式
英文:

Need XML root element without end tag using JAXB STAX in combination

问题

以下是您提供的内容的翻译:

我正在尝试将列表编组为XML。由于XML包含列表,并且我不想为外部标签创建单独的类,所以我在使用STAX和JAXB的组合。

问题是,我得到了带有结束标签的元素。
实际输出

<WRAPPER>
	<ROOT att1="val1" att2="val2"></ROOT>
	<ROOT att1="val1" att2="val2"></ROOT>
</WRAPPER>

期望输出

<WRAPPER>
    <ROOT att1="val1" att2="val2"/>
    <ROOT att1="val1" att2="val2"/>
</WRAPPER>

类文件

@XmlRootElement(name="ROOT")
@XmlAccessorType(XmlAccessType.FIELD)
public class ROOT {

	@XmlAttribute(name = "att1")
	String att1;
	
	@XmlAttribute(name = "att2")
	String att2;

	public String getAtt1() {
		return att1;
	}

	public void setAtt1(String att1) {
		this.att1 = att1;
	}

	public String getAtt2() {
		return att2;
	}

	public void setAtt2(String att2) {
		this.att2 = att2;
	}
}

方法

public static String marshal(List<ROOT> list)
	throws XMLStreamException, JAXBException, IOException {
	JAXBElement<ROOT> jaxb = null;
	String xmlString = null;
	if (!CollectionUtils.isEmpty(list)) {
		QName root = new QName("ROOT");
		XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
		StringWriter stringWriter = new StringWriter();
		
		XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(stringWriter);
		streamWriter.writeStartElement("WRAPPER");
		JAXBContext jc = JAXBContext.newInstance(ROOT.class);
		Marshaller marshaller = jc.createMarshaller();
		for (ROOT t : list) {
			jaxb = new JAXBElement<ROOT>(root, ROOT.class, t);
			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
			marshaller.marshal(jaxb, streamWriter);
		}
		streamWriter.writeEndDocument();
		xmlString = stringWriter.toString();
		stringWriter.close();
		streamWriter.close();
		}
	return xmlString;
}

问题是nillable不能应用于XmlRootElement。

英文:

I am trying to marshal list to xml. As the xml contains list and I do not want to create separate class for outer tag I am using STAX and JAXB in combination.

The issue is I am getting element with end tag.
actual output

<WRAPPER>
	<ROOT att1="val1" att2="val2"></ROOT>
	<ROOT att1="val1" att2="val2"></ROOT>
</WRAPPER>

Expected Output

<WRAPPER>
    <ROOT att1="val1" att2="val2"/>
    <ROOT att1="val1" att2="val2"/>
</WRAPPER>

Class file

@XmlRootElement(name="ROOT")
@XmlAccessorType(XmlAccessType.FIELD)
public class ROOT {

	@XmlAttribute(name = "att1")
	String att1;
	
	@XmlAttribute(name = "att2")
	String att2;

	public String getAtt1() {
		return att1;
	}

	public void setAtt1(String att1) {
		this.att1 = att1;
	}

	public String getAtt2() {
		return att2;
	}

	public void setAtt2(String att2) {
		this.att2 = att2;
	}
}

Method

public static String marshal(List<ROOT> list)
	throws XMLStreamException, JAXBException, IOException {
	JAXBElement<ROOT> jaxb = null;
	String xmlString = null;
	if (!CollectionUtils.isEmpty(list)) {
		QName root = new QName("ROOT");
		XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
		StringWriter stringWriter = new StringWriter();
		
		XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(stringWriter);
		streamWriter.writeStartElement("WRAPPER");
		JAXBContext jc = JAXBContext.newInstance(ROOT.class);
		Marshaller marshaller = jc.createMarshaller();
		for (ROOT t : list) {
			jaxb = new JAXBElement<ROOT>(root, ROOT.class, t);
			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
			marshaller.marshal(jaxb, streamWriter);
		}
		streamWriter.writeEndDocument();
		xmlString = stringWriter.toString();
		stringWriter.close();
		streamWriter.close();
		}
	return xmlString;
}

Problem is nillable cannot be applied to XmlRootElement

答案1

得分: 0

虽然从技术上讲,预期值和实际值是相等的,但我发现没有办法在不创建额外包装类的情况下修改它。

英文:

Although both expected and actual are technically equal I found no way to modify that in the way without creating additional wrapper class.

huangapple
  • 本文由 发表于 2020年8月21日 14:43:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63517760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定