Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Conditional statements and string parsing within Node using org.apache.xpath.XPathAPI

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been working on an XML parser application for a while now. However, now I have run into a couple of things which I cannot seem to get to work.

Here is a snippet of my XML read code.


Here is a snippet of my XML expression file which I am attempting to add a condition to in order to later parse my XML data file. This does currently work (it's within other beans and such of course).


Okay, so the expressions currently work as expected recalling the element requested. The setExpressions method being called stores an array of type Object.

The first thing I was looking to do was parse the returned element. Such as getting just the first three characters. To do this I attempted the following XML code change: <value>substring(/a/c,1,3)</value>. However, this throws an exception saying it is getting a #STRING instead of NODE object. I cannot seem to figure out how to pass this parsed result if it is possible.

The second thing I was looking to do was have a conditional statement based on other XML values in the document being parsed. For example, if element "/a/b" is equal to the string "AB" then we are actually looking to do "/a/d". Now for this I see things using XSL in the XML tags but it never successfully compiles let alone allow me to execute it. Any help on this would also be greatly appreciated.
 
Marshal
Posts: 25671
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<value>substring(/a/c,1,3)</value>

Well, yes, the XPath expression you have in that element does return a string, whereas "/a/c" returns a node list. So you can't pass its result to something that's expecting a node. You'll need to fix your design.

Now for this I see things using XSL in the XML tags but it never successfully compiles let alone allow me to execute it.

You started out with a lot of description, but it just seems to be background and you don't have any description of your actual problem that I can see. However my diagnosis is, instead of trying to write a generic XPath evaluator where you're going to keep hitting problems every time you try to add a new feature, why not just put those things into an XSL transformation and just run that?
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
Well, yes, the XPath expression you have in that element does return a string, whereas "/a/c" returns a node list. So you can't pass its result to something that's expecting a node. You'll need to fix your design.


Hmm. Do you think it is best that I create my own string parsers then?.. such as I accept the object "substring("/a/b",1,3) and deal with that myself in the code rather than relying on the classes doing it for me?

I'm very new to XML but understand how it works with open and close tags, attributes, values, etc. I just have run into trouble seeing as the system I am implementing this on cannot be upgraded to Java 1.5 and that's what a lot of the information on the web speaks about now since it has javax.xml.xpath built in.

Originally posted by Paul Clapham:
You started out with a lot of description, but it just seems to be background and you don't have any description of your actual problem that I can see. However my diagnosis is, instead of trying to write a generic XPath evaluator where you're going to keep hitting problems every time you try to add a new feature, why not just put those things into an XSL transformation and just run that?


I'm sorry that I got a little lax on the description. I'm simply looking to have my XPath expression perform a test on one XPath expression. Pending the result of that test, I would then want the result of one XPath or another to be the one which is actually being the returned value. Perhaps this is a better example in layman's terms.

So this code block would have to perform the /a/b query, then compare it to 7. If /a/b is in fact 7, it would get the value of /c/d and return it to the XPath query, otherwise it would get the value of /e/f and return that.
 
Paul Clapham
Marshal
Posts: 25671
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, XPath doesn't have an if() function. So now you're talking about designing your own language that's a superset of XPath. Personally, given the existence of XSLT, I wouldn't do that.
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your comments Paul.

I will look into using XSLT instead of XPath. I'm hoping there is not too much code variance between the two.
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic