473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Serializing an XML Dom

I have created an W3C XML DOM in java,
it uses namespaces.

Now i want to serialize it to the outputstream:

===
Document document=retrievePageDocument(page, manager.getDataStore());
response.setContentType("text/xml");
PrintWriter out=response.getWriter();

DOMSource domSource=new DOMSource(document);

StreamResult streamResult=new StreamResult(out);
TransformerFactory tf=TransformerFactory.newInstance();
Transformer serializer=tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING," UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "xml");
serializer.transform(domSource, streamResult);
===

The serialization works, but it does not include the namespaces!!

I also tried to use the Xerces Serialization API directly:

===
// lets serialize the xml to the output
XMLSerializer serializer=new XMLSerializer(out,
new OutputFormat(document, "UTF-8", true)
);
serializer.serialize(document);
===

But that also does not inlcude the namespaces??@?

The Document object is namespace aware:

===
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document doc;
doc=factory.newDocumentBuilder().newDocument();
Element root=doc.createElementNS(PAGE_NAMESPACE, "page");
doc.appendChild(root);
===
Pleaz help im stuck here!
Dec 1 '05 #1
1 1427


Tjerk Wolterink wrote:
I have created an W3C XML DOM in java,
it uses namespaces. The serialization works, but it does not include the namespaces!!


The following

DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder();
Document xmlDocument =
documentBuilder.getDOMImplementation().createDocum ent(
"http://www.w3.org/1999/xhtml",
"html",
null
);
Element rootElement = xmlDocument.getDocumentElement();

rootElement.setAttributeNS("http://www.w3.org/XML/1998/namespace",
"xml:lang", "en");

System.out.println("xmlDocument class: " +
xmlDocument.getClass().getName());
System.out.println("Serialized document:");
System.out.println(serializeToString(xmlDocument)) ;

run with Java 1.5 gives

xmlDocument class: com.sun.org.apache.xerces.internal.dom.DocumentImp l
Serialized document:
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"></html>

which looks fine, namespace declarations and prefixes are there.

Run with Java 1.4 however I get:

xmlDocument class: org.apache.crimson.tree.XmlDocument
Serialized document:
<?xml version="1.0" encoding="UTF-8"?>
<html xml:lang="en"/>

so you might have hit a bug with the DOM or Transformer implementation
you are using respectively which is on your classpath.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 2 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ryan Hubbard | last post by:
I've have been trying to serialize an array of objects. I can serialize and set a session variable and retrieve it successfully for a serialize string of any length within the contents of a single...
2
by: Aleksei Guzev | last post by:
Imagine one writing a class library CL1 for data storage. He defines classes ‘DataItem’ and ‘DataRecord’ so that the latter contains a collection of the former. And he derives class ‘IntItem’ from...
1
by: Ivo Bronsveld | last post by:
All, I have quite a challenging task ahead of me. I need to write an object model (for code access) based on a schema, which cannot be made into a dataset because of it's complexity. So I...
10
by: copx | last post by:
I want to save a struct to disk.... as plain text. At the moment I do it with a function that just writes the data using fprintf. I mean like this: fprintf(fp, "%d %d", my_struct.a, my_struct.b)...
1
by: Chris | last post by:
I'm having trouble Serializing a System.Data.DataColumn object. When I try to serialize it, I get the following: System.NotSupportedException: Cannot serialize member...
2
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: +...
4
by: Jason Shohet | last post by:
We are thinking of serializing an object & passing it toseveral functions on web service. This will happen about 35 times as the page loads. The class has about 20 attributes. We're not sure...
2
by: Simon | last post by:
I'm developing a new application and want to use serialization as a way to save my data. But as I add new variables to my classes, how will serializing cope with that? For example, suppose I have...
7
by: fjlaga | last post by:
I have written an Office Add-in for Excel using VB.NET and the .NET 1.1 Framework (I have Visual Studio 2003 .NET ). All works great. I want to add a User Settings/Prefereneces dialog and allow...
12
by: Cagdas Ozgenc | last post by:
Greetings, When directly serializing C++ structures to a file with the standard library functions giving the address of the data and length of structure using the sizeof operator, do I risk...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.