• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Selective Parsing Puzzle

 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is an XML selective parsing puzzle that I haven't been able to figure out.

I've got some XML source that I'm reading using SAX (ie: SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new StringReader(bpSource));Element root = doc.getRootElement(); ) I can get everything I need out of the XML, that's not the problem. It's a logic problem with picking and choosing what to include and what to discard based on a second input.

The inputs to the program are 1)an XML file, and 2) an array of numbers. Those numbers represent which choice statements will be active.

So if I had array = {2}, and XML like this:


The result would be "<sequence name = "B"><stuffB/></sequence>" (the case's activity matches up on the sequence name).

The problem comes when you have nested choices (besides just "<stuffB>", you might have "<case><select>..." in that sequence as well. In other words, the solution needs to handle "n-level deep choices".

There is an example input XML file at the bottom of this post. For that example, we might have a few different number array inputs:

if my input was {1}, my output would be:


if my input was {2, 1}, my output would be:


if my input was {2, 2, 1}, my output would be:


So, can anyone think of a smart way to assemble only the selected XML out of the whole, based on the number array inputs?

--Dale--



Here is the sample XML file that goes with the example outputs above. Some of you might recognize this a BPML (Business Process Modeling Language), but that doesn't matter, that is, unless you know of a BPML parser that does what I'm looking for!


[ May 04, 2006: Message edited by: Dale Seng ]
 
Dale Seng
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the lack of responses, I guess this problem was too hard for you folks here ;-) But it always helps me to formulate a question for the forum because it forces me to distill-out the essence of the problem. Once I did that, it became more clear what I needed to do.

The specific question was really how to do the n-level deep pruning, but I figured that out. I created a method that took parameters: a branch, plus the array of numbers representing the choices. It returned the array, which was shortened for each choice that was used. I didn't need to pass around the whole document because in jdom if you have an element, you have everything you need. The first thing in the method was to get the children of the element and check if it's a choice element type. One of the keys to the solution was the detatch() method. Then I used addcontent() to put it where the choice used to be. Then, from inside this method, I called the method again. Recursion would finally stop because we would get down to the 'leaf nodes' (ie run out of children).

--Dale--
 
reply
    Bookmark Topic Watch Topic
  • New Topic