Jump to content

Edit an XML file from JSP (XPath)

- - - - -

  • Please log in to reply
14 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello,

I need some help with this. Until now i know how to add in XML from JSP but i have never try before to edit an XML file. More specific, i would like to decrease <number>...</number> tag any time submit button is pressed.
thank you

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You should be able to do setTextContent(..) on the node your evaluate method returns, see Change a particular node in XML : DOM Edit « XML « Java

#3
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Is possible to do it at the same Manager class, or i have to create new class?

Edit: The element that i want to change is an integer type, and my goal is each time corresponding item has been selected, its quantity decrease by 1. There is any other method instead of getTextContent()?

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Not really something useful: Node (Java 2 Platform SE 5.0)

Just do

int currentValue = Integer.parseInt( node.getTextContent() );

node.setTextContent(currentValue -1);



And going to bed now.

#5
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Very helpfull thank you.

Just to see if i become good student
to compile the quantity of current item

//shop/category/items[name=='getName()']/quantity
right???

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
If you're xml looks like:

...possible other tags before

<shop>

  <category>

    <items>

      <name>

      <quantity>

yes.

#7
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
EDIT

Now the problem is not those methods, but
.setTextContent(int-1);
says cannot applied to (int). Tried
.setTextContent((int-1).toString());
but says int cannot be dereferenced.

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I think that's because int has no toString method (every class that doesn't begin with a capital letter has no methods - primitive class in Java)
Just do +""

.setTextContent((int-1) +"");



#9
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
I tried
Integer.toString(int-1)
and is working :). Page is working fine if i have for example
shop/category/items[name='APPLES']/quantity
each time apples added to the cart XML decreased by one, but how i will achieve to do it for all items?
for(int i=0; i<purchase.size(); i++){

	Purchase dlPurchase = (Purchase) purchase.get(i);

        .

        .

        shop/category/items[name='"+dlPurchase.getName()+"']/quantity

.....

}
Tried the above but nothing happen

#10
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
It is never get in the for loop..:S strange

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Then it's simple, purchase is empty :P

#12
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
ohh im so stupid... i had clearAll() before decrease() grr :s




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users