XStream - data within a tag should be indented identically

27 Views Asked by At

I could use some help to get a better XML output. I hope to find a solution here! Couldn't find any solution for this.

That is my current output:

<java.util.Arrays_-ArrayList>
  <a class="sct.commons.xml.TestObject-array">
    <sct.commons.xml.TestObject>
      <normalText>123</normalText>
      <cdataText><![CDATA[456]]></cdataText>
    </sct.commons.xml.TestObject>
    <sct.commons.xml.TestObject>
      <normalText>abc</normalText>
      <cdataText><![CDATA[def]]></cdataText>
    </sct.commons.xml.TestObject>
    <sct.commons.xml.TestObject>
      <normalText>some text</normalText>
      <cdataText><![CDATA[Lorem ipsum dolor sit amet
consectetur adipisici elit
sed eiusmod tempor incidunt ut labore et dolore magna aliqua
]]></cdataText>
    </sct.commons.xml.TestObject>
  </a>
</java.util.Arrays_-ArrayList>

But I need one of the following one:

Minimal solution:

<java.util.Arrays_-ArrayList>
  <a class="sct.commons.xml.TestObject-array">
    <sct.commons.xml.TestObject>
      <normalText>123</normalText>
      <cdataText><![CDATA[456]]></cdataText>
    </sct.commons.xml.TestObject>
    <sct.commons.xml.TestObject>
      <normalText>abc</normalText>
      <cdataText><![CDATA[def]]></cdataText>
    </sct.commons.xml.TestObject>
    <sct.commons.xml.TestObject>
      <normalText>some text</normalText>
      <cdataText><![CDATA[Lorem ipsum dolor sit amet
        consectetur adipisici elit
        sed eiusmod tempor incidunt ut labore et dolore magna aliqua
        ]]></cdataText>
    </sct.commons.xml.TestObject>
  </a>
</java.util.Arrays_-ArrayList>

Best solution:

<java.util.Arrays_-ArrayList>
  <a class="sct.commons.xml.TestObject-array">
    <sct.commons.xml.TestObject>
      <normalText>123</normalText>
      <cdataText><![CDATA[456]]></cdataText>
    </sct.commons.xml.TestObject>
    <sct.commons.xml.TestObject>
      <normalText>abc</normalText>
      <cdataText><![CDATA[def]]></cdataText>
    </sct.commons.xml.TestObject>
    <sct.commons.xml.TestObject>
      <normalText>some text</normalText>
      <cdataText>
        <![CDATA[
        Lorem ipsum dolor sit amet
        consectetur adipisici elit
        sed eiusmod tempor incidunt ut labore et dolore magna aliqua
        ]]>
      </cdataText>
    </sct.commons.xml.TestObject>
  </a>
</java.util.Arrays_-ArrayList>

I run this code:

XStream xStream = new XStream(cdataXppDriver);
String xml = xStream.toXML(model);

Following my code for the driver:

public class CdataXppDriver extends XppDriver
{

    /** The list that contains the field names that would contain a CDATA in the xml. */
    private final List<String> CDATA_FIELDS;

    public CdataXppDriver(List<String> cdataFields)
    {
        super();
        CDATA_FIELDS = new ArrayList<>();

        if (cdataFields != null)
        {
            for (String cdataField : cdataFields)
            {
                CDATA_FIELDS.add(cdataField);
            }
        }
    }

    @Override
    public HierarchicalStreamWriter createWriter(Writer out)
    {
        return new PrettyPrintWriter(out, getNameCoder())
        {
            boolean cdata = false;

            @Override
            public void startNode(String name)
            {
                super.startNode(name);
                cdata = CDATA_FIELDS.contains(name);
            }

            @Override
            protected void writeText(QuickWriter writer, String text)
            {
                if (cdata)
                {
                    writer.write("<![CDATA[");
                    writer.write(text);
                    writer.write("]]>");
                }
                else
                {
                    super.writeText(writer, text);
                }
            }
        };
    }
}

I need more text to post this question, but I don't know which informations might be missing:

0

There are 0 best solutions below