on 03-15-2007 11:27 AM
I've a DOM Document, obtained from a file this way:
File f = new File("c:/file.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document doc = builder.parse(file);
[... code to modify doc ...]
<b>How can I serialize it back to a flat file ?</b>
I tried xalan libraries:
OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream());
java.util.Properties props = OutputPropertiesFactory.getDefaultMethodProperties(Method.XML);
Serializer ser = SerializerFactory.getSerializer(props);
ser.setWriter(osw);
DOMSerializer dser = ser.asDOMSerializer(); // a DOM will be serialized
dser.serialize(document);
But it doesn't find class '<b>OutputPropertiesFactory</b>'.
Help needed.
Best regards.
Hai ,
your need
serializer.jar
you can find this jar here
http://xml.apache.org/xalan-j/downloads.html
regards,
Naga
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Anibal,
In reality it's likely more complex, but please check this link: <a href="http://www.javaalmanac.com/egs/javax.xml.transform/WriteDom.html">Writing a DOM Document to an XML File</a>
Personally I'd avoid relying on some specific XML (SAX, DOM, XPath or XSLT) library implementation. JAXP is an integral part of the J2SE platform. It's a bit difficult to find exactly which implementation are included in which J2SE version, but "For example, In the J2SE platform 5.0, Xalan has been replaced by XSLTC (an XSL stylesheet compiler)".
Kind regards,
Sigiswald
I'm applying the transformer without any template.
But I got this exception:
<b>Namespace fixup failed. Prefix 'w' used in attribute 'w:macrosPresent' is not declared anywhere</b>
My xml file is a wordML like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:st1="urn:schemas-microsoft-com:office:smarttags" w:macrosPresent="yes" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve">
<o:SmartTagType o:namespaceuri="urn:schemas-microsoft-com:office:smarttags" o:name="date"/>
<o:DocumentProperties>
[...]
Any idea?
Hi Anibal,
This looks strange... Can you try it standalone? If I use the code in your original post to convert the XML you have to a DOM and then back to a file, it works fine.
If it doesn't work standalone, is it possible to give the complete XML?
If it works fine standalone, but not on the app server, there could be a problem with the XML parsers implementation on the server.
Kind regards,
Sigiswald
I'm implementing all this in a Webdynpro project. I've NW04sSP10
I've tried too with xerces library ...\eclipse\plugins\org.apache.xerces_4.0.13/xercesImpl.jar
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
XMLSerializer serial = new XMLSerializer(os, format);
serial.serialize(doc);
But now I get: <b>java.lang.NoClassDefFoundError: org/apache/xml/serialize/BaseMarkupSerializer</b>
I've checked this class is into the jar. What now?
I'm gonna try it standalone.
I've tried standalone and both xerces and transformer works. With the only difference that xerces indent the output file.
If I try into webdynpro, with a simple xml file like:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
... transformation works.
I'm working with a WordML file, which is the Word XML format. The nodes have attributes like w:macrosPresent="yes". If I add this kind of attribute to the upper file:
<note>
<to <b>w:macrosPresent="yes"</b>>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Transformation gives: Namespace fixup failed. Prefix 'w' used in attribute 'w:macrosPresent' is not declared anywhere.
About xerces: I forgot packing xerces jar into ear Now it works with xerces!
How can I solve the problem with the prefixes and transformation?
Thanks a lot for your help.
The 2nd small XML you show isn't valid: you use a namespace but don't declare one. Can you try with the following?
<u>wordML.xml</u>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"
xmlns:aml="http://schemas.microsoft.com/aml/2001/core"
xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:st1="urn:schemas-microsoft-com:office:smarttags"
w:macrosPresent="yes"
w:embeddedObjPresent="no"
w:ocxPresent="no"
xml:space="preserve"/>
If it doesn't work, save the above xml locally as wordML.xml.
Then create the file <u>Test.java</u>
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
public class Test {
public static void main(String[] args) throws Exception {
File file = new File("wordML.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document doc = builder.parse(file);
writeXmlFile(doc, "wordML-out.xml");
}
// This method writes a DOM document to a file
public static void writeXmlFile(Document doc, String filename) throws Exception {
try {
// Prepare the DOM document for writing
Source source = new DOMSource(doc);
// Prepare the output file
File file = new File(filename);
Result result = new StreamResult(file);
// Write the DOM document to the file
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(source, result);
} catch (TransformerConfigurationException e) {
throw e;
} catch (TransformerException e) {
throw e;
}
}
}
And on the Command Prompt type
<i>javac Test.java</i>
<i>java Test</i>
That works fine by me, both with JDK 1.4.2 and JDK 6.0.
Kind regards,
Sigiswald
My code is the same as yours. I tried with that xml.
I told you, when I try it standalone like the class you wrote, it works.
But when I try it within Webdynpro it doesn't treat the namespace correctly.
Could be that default transformer isn't the same depends on where you run it?
At least I put xerces to work, although I've read in some old posts, it can causes conflicts with WAS. That's really?
Gotta go. I'll follow on Monday
Best regards.
Oh, I'm sorry, I didn't notice the first of your previous post where you say it works locally!
Let me try on Monday from within a Web Dynpro.
The whole point is of course that by using the transfomer you code against interfaces, not against implementations. So yes, the app server can indeed use a completely different implementation. Maybe SAP implemented their own parser?
I don't know about Xerces causing conflicts. I suppose it's true if you configure Xerces to be the default SAX parser (used by the complete server), but that's not what you do I assume?
Kind regards,
Sigiswald
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.