You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Todd Fuller <tf...@yahoo.com> on 2004/04/06 15:52:56 UTC

[JXPath] Setting DOM attributes using createPathAndSetValue(...)

Hi,

I'm having some trouble setting attributes in a DOM using
"createPathAndSetValue()" using JXPath v1.1. Starting with a DOM of:

<Root>
  <First>
    <Second AttrName="NewValue" />
  </First>
</Root>

I initialize the JXPathContext using:

        JXPathContext context = JXPathContext.newContext(rootElem);
        context.setFactory(new JXPathNodeFactory());
        context.setLenient(true);

(The JXPathNodeFactory creates a new Element from the Document of the
parent Element, and calls parent.appendChild(newElem).)

Running the following:

        context.createPathAndSetValue("/First/Second/Third", "foo");

I get the expected result of:

<Root>
  <First>
    <Second AttrName="NewValue">
      <Third>foo</Third>
    </Second>
  </First>
</Root>

However, if I run this line to create an attribute on Element "Third"...

        context.createPathAndSetValue("/First/Second/Third/@attr", "foo");

it throws this exception:

org.apache.commons.jxpath.JXPathException: Exception trying to create
xpath /First/Second/Third/@attr; Cannot create an attribute for path
/First[1]/Second[1]/Third/@attr, operation is not allowed for this type of
node
	at
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.createPathAndSetValue(JXPathContextReferenceImpl.java:433)
	at
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.createPathAndSetValue(JXPathContextReferenceImpl.java:421)

I guess I'm confused why the expression has enough information to create
the text node, but not enough to set an attribute. Is there something I'm
missing, or are attributes not allowed in a "createPath..." context? I
have tested them with "setValue()", and they work fine.

Thanks in advance,
--Todd

__________________________________
Do you Yahoo!?
Yahoo! Small Business $15K Web Design Giveaway 
http://promotions.yahoo.com/design_giveaway/

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org