gpt4 book ai didi

xml - Jaxb - Marshall - 如何格式化 xml 属性 - 例如,在每个属性后放置一个换行符

转载 作者:数据小太阳 更新时间:2023-10-29 01:58:04 28 4
gpt4 key购买 nike

我正在尝试找到一种格式化 xml 的方法,以便每个属性都在一个新行中。

代码:

        OutputFormat of = new OutputFormat();
of.setIndent(4);
XMLSerializer serializer = new XMLSerializer(of);
Writer stringWriter = new StringWriter();
serializer.setOutputCharStream(stringWriter);

marshaller.marshal(target, serializer.asContentHandler());
results = stringWriter.toString();

我想得到这个:

<blablabla isGood="false" newInstance="false" id="cse_a"
deleted="false" name="cse_a"
xmlns:blabla="http://www.blabla.com">

<Description><![CDATA[]]></Description>
<Name><![CDATA[A]]></Name>

</blablabla>

看起来像这样:

<blablabla isGood="false"
newInstance="false"
id="cse_a"
deleted="false"
name="cse_a"
xmlns:blabla="http://www.blabla.com">

<Description><![CDATA[]]></Description>
<DisplayName><![CDATA[A]]></DisplayName>

</blablabla>

谢谢!

最佳答案

只是 Blaise Doughan 回答的一个例子,使用 ContentHandler:

import java.io.IOException;
import java.io.Writer;

import org.apache.commons.lang3.StringUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class WriteOutContentHandler extends DefaultHandler
{

private static final String NEWLINE = System.getProperty("line.separator");
private static final String INDENT = " ";

private Writer _writer;
private int depth = 0;

public WriteOutContentHandler(Writer writer)
{
_writer = writer;
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException
{
try
{
_writer.write(ch, start, length);
} catch (IOException e)
{
throw new SAXException("Error writing out character content", e);
}
}

@Override
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException
{
try
{
_writer.write(ch, start, length);
} catch (IOException e)
{
throw new SAXException("Error writing out character content", e);
}
}

@Override
public void endDocument() throws SAXException
{
try
{
_writer.flush();
} catch (IOException e)
{
throw new SAXException("Error flushing character output", e);
}
}

@Override
public String toString()
{
return _writer.toString();
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attrs) throws SAXException
{
write(NEWLINE);
write(StringUtils.repeat(INDENT, depth));
depth++;

String eName = localName;

if ("".equals(eName))
{
eName = qName;
}

write("<" + eName);

if (attrs != null)
{
for (int i = 0; i < attrs.getLength(); i++)
{
String attrName = attrs.getLocalName(i);

if ("".equals(attrName))
{
attrName = attrs.getQName(i);
}

write(NEWLINE);
write(StringUtils.repeat(INDENT, depth));
write(attrName);

write("=\"");
write(attrs.getValue(i));
write("\"");
}
}

if (attrs.getLength() > 0)
{
write(NEWLINE);
write(StringUtils.repeat(INDENT, depth-1));
}

write(">");
}

@Override
public void endElement(String namespaceURI, String sName, String qName) throws SAXException
{
write(NEWLINE);
depth--;
write(StringUtils.repeat(INDENT, depth));

String eName = sName;
if ("".equals(eName))
{
eName = qName;
}
write("</" + eName + ">");
}

private void write(String s) throws SAXException
{
try
{
_writer.write(s);
_writer.flush();
} catch (IOException e)
{
throw new SAXException("I/O error", e);
}
}
}

和用法:

    StringWriter writer = new StringWriter();
JAXBContext jc = JAXBContext.newInstance(MODEL);
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(node, new WriteOutContentHandler(writer));

return writer.toString();

关于xml - Jaxb - Marshall - 如何格式化 xml 属性 - 例如,在每个属性后放置一个换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17260371/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com