question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to serialize Java bean with empty element to xml with start and end tag instead of self-closing tag

See original GitHub issue

when serialize to xml,I need <OrderId><OrderId/> instead of <OrderId/> ,how to do it?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
anand1stcommented, Sep 17, 2020

Thank you. Confirmed that the following does the trick. outputFactory.setProperty(XMLOutputFactory2.P_AUTOMATIC_EMPTY_ELEMENTS, Boolean.FALSE);.

2reactions
AniFengxcommented, Sep 12, 2018

I set the property but it doesn’t work. code :

CorpGoodsReport g = new CorpGoodsReport();
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.getFactory().getXMLOutputFactory().setProperty(WstxOutputProperties.P_AUTOMATIC_END_ELEMENTS, false);
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
xmlMapper.writeValue(new FileOutputStream("a.xml"), g);

But if field is null ,the xml tag will be <FIELD_CHAR3/> ,if field is “” ,XmlMapper can generate normally
<FIELD_CHAR1></FIELD_CHAR1>. Do you have any idea can fix it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[jackson-user] how to serialize Java bean with empty element ...
[jackson-user] how to serialize Java bean with empty element to xml with start and end tag instead of self-closing.
Read more >
How to generate self-closing tag <tag/> for empty element in ...
When I marshal the entity to a XML string with: StringWriter sw = new StringWriter(); jaxbMarshaller.marshal(countryDataObject, sw); sw.toString ...
Read more >
Start and end tag (empty) instead of self closing tags in xml
Hi. Is there any way to have empty tags i.e <tag_name></tag_name> instead of self closing tags i.e <tag_name/> while using tAdvancedOutputFileXml component.
Read more >
Solving the XML Problem with Jackson - Stackify
Looking for a mature, flexible way of working with both JSON and XML for the same data? Read how the Jackson XML module...
Read more >
Automatic replacement of closing tags - Oracle Communities
So, it should not illegal to have an empty element with a start and closing tag in XML. I would like to find...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found