[jdom-interest] java.lang.IllegalStateException
Malachi de AElfweald
malachi at tremerechantry.com
Sun Oct 13 12:21:33 PDT 2002
Try something like this:
JDOMResult result = new JDOMResult();
transformer.transform(new JDOMSource(in), result);
Document doc = result.getDocument();
10/13/2002 6:39:08 AM, "Robert Douglass" <r.douglass at onlinehome.de> wrote:
>Hello JDOM list,
>
>I'm getting the following error
>
>java.lang.IllegalStateException: Root element not set
> at org.jdom.Document.getContent(Document.java:332)
> at org.jdom.output.XMLOutputter.output(XMLOutputter.java:794)
> at org.jdom.output.XMLOutputter.output(XMLOutputter.java:585)
> at sbpgames.util.XmlUtil.applyXPath(XmlUtil.java:39)
> at sbpgames.util.XmlUtil.main(XmlUtil.java:128)
>
>when I attempt to use XMLOutputter.output on a document that is a result of
>an in memory Transformer transformation. The main method (at the bottom)
>creates a Document and passes that, along with an XPath expression as a
>String to applyXPath (first method in the class). applyXPath creates a xsl
>document that matches / and selects the XPath expression fed to it. This
>stylesheet and the xml document are then fed to transform(JDOMSource in,
>Source stylesheet, which returns a Document. If I change this line in the
>transform method
>
> JDOMResult out = new JDOMResult();
>
>to
>
> Result out = new StreamResult(System.out);
>
>then I see that the transformation is working as expected. Therefore, I
>suspect that I don't understand what happens to out in
>transformer.transform(in, out);
>
>I'm new to this technology, so I realize my error could be anywhere, and
>that this is a long chunk of code. Therefore I am very grateful to anybody
>who has the time to look at it.
>
>-Robert Douglass
>
>
>
>public class XmlUtil {
> private static XMLOutputter xmlOutputter = new XMLOutputter(" ", true);
>
> public static synchronized Document applyXPath(Document doc, String path) {
> Document result = null;
> try {
> JDOMSource xmlSource = new JDOMSource(doc);
> // returns a stylesheet that matches / and selects the path in "path"
> Document xsltSourceDoc = getStyleSheetWithTemplateElement(path);
> Source xsltSource = new JDOMSource(xsltSourceDoc);
>
> // output the two documents to System.out
> System.out.println("doc:");
> xmlOutputter.output(doc, System.out);
> System.out.println("xsltSourceDoc:");
> xmlOutputter.output(xsltSourceDoc, System.out);
>
> // transformation
> result = transform(xmlSource, xsltSource);
> } catch (Exception e) {
> e.printStackTrace();
> }
> return result;
> }
> public static synchronized Document transform(
> JDOMSource in,
> Source stylesheet)
> throws JDOMException {
> try {
> Transformer transformer =
> TransformerFactory.newInstance().newTransformer(stylesheet);
> JDOMResult out = new JDOMResult();
> transformer.transform(in, out);
> return out.getDocument();
> } catch (TransformerException e) {
> throw new JDOMException("XSLT Trandformation failed", e);
> }
> }
>
> public static synchronized Document getStyleSheetWithTemplateElement(String
>path) {
> Document result = getEmptyXmlStyleSheet();
> Element templateElement =
> new Element("template", result.getRootElement().getNamespace());
> templateElement.setAttribute("match", "/");
> Element valueOfTemplate =
> new Element("value-of", result.getRootElement().getNamespace());
> valueOfTemplate.setAttribute("select", path);
> templateElement.addContent(valueOfTemplate);
> result.getRootElement().addContent(templateElement);
> return result;
> }
>
> public static synchronized Document getEmptyStyleSheet() {
> Element root =
> new Element(
> "stylesheet",
> "xsl",
> "http://www.w3.org/1999/XSL/Transform");
> root.setAttribute("version", "1.0");
> Document result = new Document(root);
> return result;
> }
>
> public static synchronized Document getEmptyXmlStyleSheet() {
> Document result = getEmptyStyleSheet();
> result = addOutputElement(result, "xml");
> return result;
> }
>
> private static synchronized Document addOutputElement(
> Document result,
> String method) {
> Element outputElement =
> new Element("output", result.getRootElement().getNamespace());
> outputElement.setAttribute("method", method);
> result.getRootElement().addContent(outputElement);
> return result;
> }
>
> public static void main(String[] args) {
> try {
> // create a simple XML document
> Element root = new Element("Dog");
> Element node = new Element("Name");
> node.setAttribute("Age", "7");
> node.setText("Fifi");
> root.addContent(node);
> Document doc = new Document(root);
>
> // goal is to get "7" from above
> Document pathApplied = applyXPath(doc, "/Dog/Name/@*");
>
>/////////////////// error produced here //////////////////////
> xmlOutputter.output(pathApplied, System.out);
>//////////////////////////////////////////////////////////////
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
>}
>
>_______________________________________________
>To control your jdom-interest membership:
>http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
>
>
More information about the jdom-interest
mailing list