XML Parsing Puzzle

D

DRS.Usenet

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:
<choice>
<select>
<case activity="A"/> <!-- this is "1" -->
<case activity="B"/> <!-- this is "2" -->
</select>
<sequence name = "A"><stuffA/></sequence>
<sequence name = "B"><stuffB/></sequence>
</choice>

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:
<sequence name = "Run Option 1">
<assign to="/Some1">run option 1 was chosen</assign>
</sequence>

if my input was {2, 1}, my output would be:
<sequence name = "Run Option 2">
<assign to="/Some2">run option 2 was chosen</assign>
<sequence name="Simple1">
<assign to="/SomeVar1">simple 1 was chosen</assign>
</sequence>
<assign to="/Some2b">run option 2 ending</assign>
</sequence> <!-- end Run Option 2 -->


if my input was {2, 2, 1}, my output would be:
<sequence name = "Run Option 2">
<assign to="/Some2">run option 2 was chosen</assign>
<sequence name="Deep Nested">
<sequence name="Run Deep 1">
<assign to="/D1">d1</assign>
</sequence>
</sequence>
<assign to="/Some2b">run option 2 ending</assign>
</sequence> <!-- end Run Option 2 -->

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!

<process name="aProcess">
<sequence name="Main Sequence">

<choice name="Outer Choice">
<select>
<case ref="option 1" activity="Run Option 1"/>
<case ref="option 2" activity="Run Option 2"/>
</select>
<sequence name = "Run Option 1">
<assign to="/Some1">run option 1 was chosen</assign>
</sequence>
<sequence name = "Run Option 2">
<assign to="/Some2">run option 2 was chosen</assign>

<choice name="Nested choice">
<select>
<case ref="option 1" activity="Simple1"/>
<case ref="option 2" activity="Deep Nested"/>
<case ref="option 3" activity="Simple2"/>
</select>
<sequence name="Simple1">
<assign to="/SomeVar1">simple 1 was chosen</assign>
</sequence>
<sequence name="Deep Nested">

<choice name="It's getting deep">
<select>
<case ref="deepoption 1" activity="Run Deep 1"/>
<case ref="deepoption 2" activity="Run Deep 2"/>
</select>
<sequence name="Run Deep 1">
<assign to="/D1">d1</assign>
</sequence>
<sequence name="Run Deep 2">
<assign to="/D2">d2</assign>
</sequence>
</choice> <!-- end Deep Nested choice -->

</sequence>
<sequence name="Simple2">
<assign to="/SomeVar2">simple 2 was chosen</assign>
</sequence>
</choice> <!-- end Nested choice -->

<assign to="/Some2b">run option 2 ending</assign>
</sequence> <!-- end Run Option 2 -->
</choice> <!-- end Outer Choice -->

</sequence> <!-- end Main Sequence -->
</process>
 
P

Patricia Shanahan

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.
....

Unless the original file is VERY big, I would split this into two problems:

1. Parse the whole XML file, building an internal tree representation,
using e.g. JDOM.

2. Scan the internal representation, top down, pruning out the select
and the things that were not selected at each choice. Increment the
array index each time you use a number to make a decision.

Patricia
 
D

DRS.Usenet

Thanks Patricia, that's just what I did. I 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.

No, the files won't be big at all. 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--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top