[prev in list] [next in list] [prev in thread] [next in thread]
List: xerces-j-user
Subject: Serializing Attribute nodes
From: "Desmond Whewell \(CV/ETL\)" <desmond.whewell () ericsson ! com>
Date: 2007-03-30 12:29:04
Message-ID: 94BF386B051B5849BA84A45DDFC4E60AB8B53B () esealmw118 ! eemea ! ericsson ! se
[Download RAW message or body]
I have a requirement to serialize parts of an XML document, either the
whole document or an element or an attribute. My code works fine for
documents and elements, but fails for attributes. If I try to serialize
an attribute node, no output is generated. I expected the value of the
attribute to be written, e.g. for <... Fred="hello"> , I expected
"hello" to be serialized.
Why does this not happen?
I am using xerces 2.8.1 and Java 1.5_04
Here is some test code that reflects my usage.
START_OF_CODE
package test;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args)
throws Exception {
// Dummy document
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
sb.append("<wrapperRoot>");
sb.append("<wrapperElem wrapperAtt=\"hello\">");
sb.append("</wrapperElem>");
sb.append("</wrapperRoot>");
// Parse dummy document
Document newdoc = null;
try {
DocumentBuilderFactory fa =
DocumentBuilderFactory.newInstance();
fa.setNamespaceAware(true);
fa.setFeature(
"http://apache.org/xml/features/dom/defer-node-expansion",
false);
DocumentBuilder parser = null;
parser = fa.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(
sb.toString()));
inputSource.setEncoding("UTF-8");
newdoc = parser.parse(inputSource);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
// Document - This works
doSerialize(newdoc, true);
System.out.println();
// Root Element- This works
doSerialize(newdoc.getDocumentElement(), false);
System.out.println();
// Element - This works
doSerialize(newdoc.getDocumentElement().getFirstChild(), false);
System.out.println();
// Attribute - This does not work, nothing is written to 'out'
doSerialize(newdoc.getDocumentElement().getFirstChild().getAttributes().
item(0), false);
System.out.println();
}
public static void doSerialize(Node node, boolean xmlDecl)
throws Exception {
// Get DOM Implementation using DOM Registry
System.setProperty(DOMImplementationRegistry.PROPERTY,
"org.apache.xerces.dom.DOMXSImplementationSourceImpl");
DOMImplementationRegistry registry;
registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS)
registry.getDOMImplementation("LS");
// Create and configure an LSSerializer
LSSerializer lSSerializer = impl.createLSSerializer();
DOMConfiguration config = lSSerializer.getDomConfig();
config.setParameter("xml-declaration", xmlDecl);
config.setParameter("format-pretty-print", false);
lSSerializer.setNewLine("\n");
// Create an output sink
LSOutput lSOutput = impl.createLSOutput();
lSOutput.setByteStream(System.out);
lSOutput.setEncoding("UTF-8");
// Serialise...
lSSerializer.write(node, lSOutput);
}
}
END_OF_CODE
---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic