Enguia
Enguia

Reputation: 31

XML endTag Line Break

I am trying to create an XML document from Android, and I'm facing some problems. Here is some of my code:

XmlSerializer xml = Xml.newSerializer();
StringWriter writer = new StringWriter(); xml.setOutput(writer);
xml.startDocument("UTF-8", true);
xml.startTag("" , "EDbAuditoria");
xml.startTag("", "AFSeq");  xml.text(this.getAFSeq()); xml.endTag("", "AFSeq");
xml.startTag("", "Data");   xml.text(this.getData());  xml.endTag("", "Data");   

This works fine and generates the file, but when checking the file .. there are no line breaks between tags .. Here follows some lines of the generated file:

No Line Breaks between Tags

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><EDbAuditoria><AFSeq>LIPO20130709204106R43540</AFSeq><Data>09/07/2013</Data>

It should be created as ...

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<EDbAuditoria>
<AFSeq>98184ESTAGIARIO22013020502:4423307</AFSeq>
<Data>05/02/2013</Data>
<Hora>02:44 pm</Hora>
<Auditor>ESTAGIARIO2</Auditor>
<Shopping>027</Shopping>
<Loja>108</Loja>
<Qtd>2</Qtd>

Upvotes: 3

Views: 532

Answers (2)

yonojoy
yonojoy

Reputation: 5566

In my code I use

xml.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

after startDocument. That should give you:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<EDbAuditoria>
    <AFSeq>98184ESTAGIARIO22013020502:4423307</AFSeq>
    <Data>05/02/2013</Data>
    <Hora>02:44 pm</Hora>
    ...

Currently I cannot find it in the documentation, only example code. But for me it works.

Upvotes: 1

Shaun McCance
Shaun McCance

Reputation: 474

Line breaks and other whitespace aren't necessarily insignificant in any random XML vocabulary. If you want automatic indentation, you have to explicitly tell the serializer to add it.

xml.setOutputFormat(new OutputFormat("XML", "UTF-8", true));

Upvotes: 0

Related Questions