org.w3c.dom (XML) Automatic reordering of attributes on save

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:p w:rsidR="00FD36E9" w:rsidRPr="00823E40" w:rsidRDefault="00FD36E9"
w:rsidP="001233F6">

is reformatted as:

<w:p 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;
}
 
A

Alan

OK. After some investigation, the images are messed up even if I
simply open, save and close the file with no changes. Most of the
image is covered by a big, grey block.

Here is the code I am using at this point, in addition to the code
I posted previously (see above).

Thanks, Alan

public static void removeHyperlinks(String DocxFileName) throws
Exception
{
File file = new File(DocxFileName);
ZipFile DocxFile = new ZipFile(file);
if (file.exists())
{
OpenXmlFile DocxPack = new
OpenXmlFile(DocxFile.getName());
Document doc =
DocxPack.getOpenXMLdoc("word","document.xml");

OpenXmlFile.saveDocXfile(DocxFile, doc);
DocxFile.close();
}
}
 
A

Alan

OK, it looks like a more probable symptom of the problem is that, when
I save the zipped files, there is a JPEG file there (the image), which
is drastically smaller in the saved version as compared to the
original version.

Is there some reason that the code above would not work for
images?

Thanks, Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top