我写了这段代码。它很好用。但我的输出字符串有问题。
public static String CreateIndexForImage()
throws IllegalArgumentException, IllegalStateException, IOException
{
String Image_Name = "Bla BLa";
static XmlSerializer xmlSerializer = Xml.newSerializer();
static StringWriter writer = new StringWriter();
xmlSerializer.setOutput(writer);
xmlSerializer.startDocument("UTF-8", true);
xmlSerializer.startTag("", "imagefile");
xmlSerializer.startTag("", "image");
xmlSerializer.startTag("", "name");
xmlSerializer.text(Image_Name);
xmlSerializer.endTag("", "name");
xmlSerializer.endTag("", "image");
xmlSerializer.endTag("", "imagefile");
xmlSerializer.endDocument();
return writer.toString();
}
输出如下:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><imagefile><image><name>Bla Bla</name></image></imagefile>
但我希望输出是这样排序的:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<imagefile>
<image>
<name>Bla BLa</name>
</image>
</imagefile>
在 XmlSerializer 类或 StringWriter 类中有这样的方法或东西吗?否则我可以用另一种方式来订购上面的行。