Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

XML Parsing Puzzle

Thread view: 
DRS.Usenet@sengsational.com - 04 May 2006 18:42 GMT
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>
Patricia Shanahan - 06 May 2006 16:11 GMT
> There is an XML selective parsing puzzle that I haven't been able to
> figure out.
[quoted text clipped - 9 lines]
> 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
DRS.Usenet@sengsational.com - 08 May 2006 13:10 GMT
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--


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.