A
Alan
When I save XML documents that are then Zipped up (Word 2007), the
attributes of each XML tag are sorted in alphabetic order, unlike the
original. For example, the original line:
<w w:rsidR="00FD36E9" w:rsidRPr="00823E40" w:rsidRDefault="00FD36E9"
w:rsidP="001233F6">
is reformatted as:
<w w:rsidP="001233F6" w:rsidR="00FD36E9" w:rsidRDefault="00FD36E9"
w:rsidRPr="00823E40">
Is there any way to prevent this reordering?
To make a long story short, this is messing up some colors of
graphics in the resulting Zip (Word) file.
Below I show the code where I save the zipped XML
files. Thanks, Alan
public static void saveDocXfile(ZipFile docxFile, Document doc)
throws Exception
{
Transformer t =
TransformerFactory.newInstance().newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
t.transform(new DOMSource(doc), new StreamResult(baos));
ZipOutputStream docxOutFile = new ZipOutputStream(new
FileOutputStream(
"XMLoutput.docx"));
Enumeration entriesIter = docxFile.entries();
while (entriesIter.hasMoreElements())
{
ZipEntry entry = (ZipEntry)entriesIter.nextElement();
if (entry.getName().equals("word/document.xml"))
{
byte[] data = baos.toByteArray();
docxOutFile.putNextEntry(new
ZipEntry(entry.getName()));
docxOutFile.write(data, 0, data.length);
docxOutFile.closeEntry();
}
else
{
InputStream incoming = docxFile.getInputStream(entry);
int blocks = getByteArraySize(entry.getSize());
byte[] data = new byte[1024 * blocks];
int readCount = incoming.read(data, 0, data.length);
docxOutFile.putNextEntry(new
ZipEntry(entry.getName()));
docxOutFile.write(data, 0, readCount);
docxOutFile.closeEntry();
}
}
docxOutFile.close();
}
private static int getByteArraySize (long EntrySize)
{
long size = 0;
size = (long)((double)EntrySize / (double)1024) + 1;
return (int)size;
}
attributes of each XML tag are sorted in alphabetic order, unlike the
original. For example, the original line:
<w w:rsidR="00FD36E9" w:rsidRPr="00823E40" w:rsidRDefault="00FD36E9"
w:rsidP="001233F6">
is reformatted as:
<w w:rsidP="001233F6" w:rsidR="00FD36E9" w:rsidRDefault="00FD36E9"
w:rsidRPr="00823E40">
Is there any way to prevent this reordering?
To make a long story short, this is messing up some colors of
graphics in the resulting Zip (Word) file.
Below I show the code where I save the zipped XML
files. Thanks, Alan
public static void saveDocXfile(ZipFile docxFile, Document doc)
throws Exception
{
Transformer t =
TransformerFactory.newInstance().newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
t.transform(new DOMSource(doc), new StreamResult(baos));
ZipOutputStream docxOutFile = new ZipOutputStream(new
FileOutputStream(
"XMLoutput.docx"));
Enumeration entriesIter = docxFile.entries();
while (entriesIter.hasMoreElements())
{
ZipEntry entry = (ZipEntry)entriesIter.nextElement();
if (entry.getName().equals("word/document.xml"))
{
byte[] data = baos.toByteArray();
docxOutFile.putNextEntry(new
ZipEntry(entry.getName()));
docxOutFile.write(data, 0, data.length);
docxOutFile.closeEntry();
}
else
{
InputStream incoming = docxFile.getInputStream(entry);
int blocks = getByteArraySize(entry.getSize());
byte[] data = new byte[1024 * blocks];
int readCount = incoming.read(data, 0, data.length);
docxOutFile.putNextEntry(new
ZipEntry(entry.getName()));
docxOutFile.write(data, 0, readCount);
docxOutFile.closeEntry();
}
}
docxOutFile.close();
}
private static int getByteArraySize (long EntrySize)
{
long size = 0;
size = (long)((double)EntrySize / (double)1024) + 1;
return (int)size;
}