• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

XML as calculator input

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to use SAX to read an expression in from XML and then calculate it. However I can't get my head around this as you can't move around the XML tree like you can in DOM. Below is a sample XML document and my java class so far. Anyone got any suggestions or examples?







 
Sheriff
Posts: 22037
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A recursive set of methods comes into mind. In pseudo code:
When you encounter an element start you call eval on that element. When you encounter its matching element end you exit the method with the result.
 
Robin Heard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.



You would not be able to do this in SAX would you since there is no tree? So far I have only been able to read through the XML document and extract the relevant values. However, it does it in order of occurrence.

For example:

<expression>
<plus>
<times>
<int value="7"/>
<minus>
<int value="4"/>
<int value="24"/>
</minus>
</times>
<int value="7"/>
</plus>
</expression>

comes out as
expression
plus
times
7
minus
4
24
minus close
times close
7
plus close
expression close

 
Marshal
Posts: 26092
71
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would do it exactly as if you were parsing the equivalent string sequentially. When you see the start of a <plus> element you push that onto a stack. You can't do anything with it until you read its operands, which come next. Keep pushing things onto the stack until what you see on top of the stack is two operands with an operator below them. When you see that, pop them off and push the result of the operator back onto the stack. Repeat this until you have reached the end of the document, at which point the stack (after your processing) should contain a single entry, which is the result.
 
Rob Spoor
Sheriff
Posts: 22037
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Robin Heard wrote:Thanks for your reply.



You would not be able to do this in SAX would you since there is no tree? So far I have only been able to read through the XML document and extract the relevant values. However, it does it in order of occurrence.


I said it was pseudo code, right? You must translate that pseudo code into real code. In this case, a child element is the occurrence of a start tag. Your method will read from the parser, and depending on what it encounters will do one of the following:
- a start tag: call the appropriate method (group this in eval to keep the splitting logic located in one method)
- the end tag: return the calculated value
- anything else: throw an exception as the XML does not meet your expectations
 
Robin Heard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But how can I call eval(child); within the eval method? The element that is initially parsed into the eval method has no link to any sub links. Sorry if I'm missing the obvious here.
 
Rob Spoor
Sheriff
Posts: 22037
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget my initial input. I've worked on it and it doesn't work as I thought. Use the stack solution suggested by Paul instead.

I've got it working with one Deque<Operator>, with Operator being an abstract class that represents the operators: times, plus and minus, but also int and expression. When you encounter a start element you push a matching operator instance onto the stack (int is given its value right here since that's the only place where you have access to the attribute). When you encounter an end element you get the operator from the stack, get its calculated value, and apply that value to the now-top operator. If there are no more operators you've ended the root element, and you're done.
 
Robin Heard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit confused now. Has anyone got any examples they could post.
 
Rob Spoor
Sheriff
Posts: 22037
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In pseudo code:
It's not going to be exactly this easy; especially minus is harder. Whereas for plus you can use the fact that x + y == 0 + x + y, and for times the fact that x * y == 1 * x * y, there is no such thing for minus (or divide).
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic