First, I would suggest you cast to net.sf.saxon.om.NodeInfo rather than to
net.sf.saxon.tinytree.TinyNodeImpl - NodeInfo is a much more stable and public
interface.
The simplest interface to serialize a NodeInfo object as XML is the static
method net.sf.saxon.query.QueryResult.serialize(NodeInfo node). This sets the
serialization options to indent=yes, omit-xml-declaration=yes, which appears
to be what you are looking for. If you want more control, there is also a
four-argument serialize() method in the same class.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-04-26
Michael
Thank you very much for the help it solve my problem.
Sincerely,
Gerald
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-04-26
Hi Michael,
Got another question in returning the values of an attribute, is there a way
for Saxon to return the values of an attribute in an XPath?
Configuration config = new Configuration();
config.setLineNumbering(true);
config.setAllNodesUntyped(true);
XPathEvaluator xpath = new XPathEvaluator();
try {
StringReader strRead = new StringReader("<Foo><Bar><Bazsample=\"id1\">1</Baz><Baz>2</Baz></Bar><Bar><Baz>3</Baz><Baz>4</Baz><Baz>5</Baz><Baz>6</Baz></Bar></Foo>");
List results = (List) xpath.evaluate("/Foo/Bar/Baz/@sample", new StreamSource(strRead), XPathConstants.NODESET);
for (int i=0; i<results.size();i++){NodeInfoobject =(NodeInfo)results.get(i);Propertiesprops =newProperties();props.setProperty(OutputKeys.METHOD,"text");props.setProperty(OutputKeys.ENCODING,"UTF-8");props.setProperty("method","xml");props.setProperty(OutputKeys.INDENT,"yes");props.setProperty(OutputKeys.STANDALONE,"true");props.setProperty(OutputKeys.MEDIA_TYPE,"text");props.setProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");props.setProperty(SaxonOutputKeys.WRAP,"yes");StringWritersw =newStringWriter();QueryResult.serialize(object,newStreamResult(sw),props);System.out.println(sw.toString());System.out.println("");}}catch(Exceptionex){ex.printStackTrace();}finally{}
Exception
net.sf.saxon.event.NoOpenStartTagException: Cannot create an attribute node (sample) whose parent is a document node
at net.sf.saxon.event.NoOpenStartTagException.makeNoOpenStartTagException(NoOpenStartTagException.java:49)
at net.sf.saxon.event.TreeReceiver.attribute(TreeReceiver.java:189)
at net.sf.saxon.tinytree.TinyAttributeImpl.copy(TinyAttributeImpl.java:185)
at net.sf.saxon.event.TreeReceiver.append(TreeReceiver.java:295)
at net.sf.saxon.query.QueryResult.serializeSequence(QueryResult.java:187)
at net.sf.saxon.query.QueryResult.serialize(QueryResult.java:116)
at com.ti.itg.peit.core.xml.util.XMLUtil.main(XMLUtil.java:110)
Thank you very much.
Sincerely,
Gerald
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi!
I'm currently converting some xml manipulation code to use Saxon and got a
problem in returning the result of Saxon9 Xpath in a form of a string.
Example XML input
Xpath used: /Foo/Bar
Below code:
We need to get the output as a string.
<Bar>
<Baz>1</Baz>
<Baz>2</Baz>
</Bar>
<Bar>
<Baz>3</Baz>
<Baz>4</Baz>
<Baz>5</Baz>
<Baz>6</Baz>
</Bar>
Thank you very much for the help.
Sincerely,
Gerald.
First, I would suggest you cast to net.sf.saxon.om.NodeInfo rather than to
net.sf.saxon.tinytree.TinyNodeImpl - NodeInfo is a much more stable and public
interface.
The simplest interface to serialize a NodeInfo object as XML is the static
method net.sf.saxon.query.QueryResult.serialize(NodeInfo node). This sets the
serialization options to indent=yes, omit-xml-declaration=yes, which appears
to be what you are looking for. If you want more control, there is also a
four-argument serialize() method in the same class.
Michael
Thank you very much for the help it solve my problem.
Sincerely,
Gerald
Hi Michael,
Got another question in returning the values of an attribute, is there a way
for Saxon to return the values of an attribute in an XPath?
XML Input
Xpath: /Foo/Bar/Baz/@sample
Below Code:
Exception
Thank you very much.
Sincerely,
Gerald