[jdom-interest] java.lang.IllegalStateException

Norrman Per per.norrman at canovia.se
Mon Oct 14 02:00:48 PDT 2002


Hi,

Your transformation produces this:

  <?xml version="1.0" encoding="UTF-8"?>
  7

which is malformed xml. There is no root element and
the exception is right on the spot. Try modifying your
stylsheet to something like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output method="xml" />
  <xsl:template match="/">
    <value>
      <xsl:value-of select="/Dog/Name/@*" />
    </value>
  </xsl:template>
</xsl:stylesheet>

and your result should come out like so:

  <?xml version="1.0" encoding="UTF-8"?>
  <value>7</value>

/pmn


-----Original Message-----
From: Robert Douglass
To: Jdom List
Sent: 2002-10-13 15:39
Subject: [jdom-interest] java.lang.IllegalStateException

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@you
rhost.com
###########################################

This message has been scanned by F-Secure Anti-Virus for Microsoft Exchange.
For more information, connect to http://www.F-Secure.com/



More information about the jdom-interest mailing list