I'm using JXPath for some read only work right now and it's quite helpful. However, now that I'm trying to do some writes to a simple bean with a JDOM Document inside it, things are starting to get trickier. I'm using a JXPath nighly build from a few days ago.

I've attached a short sample of what I'm trying to do in the hope that someone can take a look at it and guide me.

In essence, I'm having trouble using the context.setValue() method and I'm getting exceptions like this:

org.apache.commons.jxpath.JXPathException: Exception trying to set value with xpath /dataXML/address/street; org.jdom.Element.addContent(Lorg/jdom/Text;)Lorg/jdom/Element;
at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.setValue(JXPathContextReferenceImpl.java:421)
at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.setValue(JXPathContextReferenceImpl.java:412)
at com.whirlycott.samples.JXPathTest.main(JXPathTest.java:35)
Exception in thread "main"


Thanks in advance,
phil.

--
                                  Whirlycott
                                  Philip Jacob
                                  [EMAIL PROTECTED]
                                  http://www.whirlycott.com/phil/
/*
 * Created on Jul 11, 2004 by pjacob
 *
 */
package com.whirlycott.samples;

import java.io.StringReader;

import org.apache.commons.jxpath.JXPathContext;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

/**
 * @author pjacob
 *
 */
public class JXPathTest {
        
        private String data = "<?xml version=\"1.0\" ?><address><street>Orchard 
Road</street></address>";
        private String firstName;
        
        public static void main(String[] args) {
                JXPathTest test = new JXPathTest();
                
                JXPathContext context = JXPathContext.newContext( test );
                
                //This works fine.
                debug(context.getValue("/dataXML/address/street"));
                
                //This doesn't.  Why not?
                Element address = new Element("address");
                address.setText("123 Happy Street");

                context.setValue("/dataXML/address/street", address);
                
                //Print the value to see if it's changed.
                debug(context.getValue("/dataXML/address/street"));
        }
        
        /**
         * @param value
         */
        private static void debug(Object value) {
                System.out.println(value);
        }

        /**
         * Convert a String (of XML) into a Document
         * @param _xml
         * @return
         */
        public Document buildDocumentFromString(String _xml) {
                SAXBuilder builder = new SAXBuilder();
                Document doc = null;
                try {
                        doc = builder.build(new StringReader(_xml));
                } catch (Exception e) {
                        debug(e.getMessage());
                        e.printStackTrace();
                }
                return doc;
        }
        
        /*
         * Bean methods
         */
        
        public Document getDataXML() {
                return buildDocumentFromString( getData() );
        }

        private String getData() {
                return data;
        }
        
        public String getFirstName() {
                return firstName;
        }
        
        public void setFirstName(String firstName) {
                this.firstName = firstName;
        }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to