Solved

Need to retrieve partial text from the value of a tag in an xml document using the SAX parsing method in Java

Posted on 2008-10-27
4
270 Views
Last Modified: 2013-11-23
Dear fellow Java/XML developers:

I am trying to parse an xml document using SAX where I am trying to retrieve the PARTIAL contents of a tag.  For example (I have attached a sample xml document), I would like to retrieve JUST THE ACT from the reference tag (eg. ACT 1, SCENE 1) and NOT THE ENTIRE VALUE OF THE TAG (eg. ACT 1, SCENE 1, SECTION 1).  In otherwords, I would like to retrieve all the text from the beginning, up to BUT NOT INCLUDING the second comma.  Is this possible?  

Thanks in advance to all who reply.
Sincerely;
Fayyaz
<PLAY>

<TITLE>The Tragedy of Hamlet, Prince of Denmark</TITLE>

<SCENE>

<SCENE-COMMENTARY>Here is where I will include commentary on the scene that follows, which I would also like to make searchable to the user.Whether 'tis nobler in the mind to suffer

The slings and arrows of outrageous fortune,

Or to take arms against a sea of troubles,

And by opposing end them?</SCENE-COMMENTARY>

<SPEECH>

<REFERENCE>ACT 1, SCENE 1, SECTION 1</REFERENCE>

<SPEAKER>LORD POLONIUS</SPEAKER>

<LINES>Yet here, Laertes! aboard, aboard, for shame!

The wind sits in the shoulder of your sail,

And you are stay'd for. There; my blessing with thee!

And these few precepts in thy memory

See thou character. Give thy thoughts no tongue,

Nor any unproportioned thought his act.

Be thou familiar, but by no means vulgar.

Those friends thou hast, and their adoption tried,

Grapple them to thy soul with hoops of steel;

But do not dull thy palm with entertainment

Of each new-hatch'd, unfledged comrade. Beware

Of entrance to a quarrel, but being in,

Bear't that the opposed may beware of thee.

Give every man thy ear, but few thy voice;

Take each man's censure, but reserve thy judgment.

Costly thy habit as thy purse can buy,

But not express'd in fancy; rich, not gaudy;

For the apparel oft proclaims the man,

And they in France of the best rank and station

Are of a most select and generous chief in that.

Neither a borrower nor a lender be;

For loan oft loses both itself and friend,

And borrowing dulls the edge of husbandry.

This above all: to thine ownself be true,

And it must follow, as the night the day,

Thou canst not then be false to any man.

Farewell: my blessing season this in thee!</LINES>

</SPEECH>

<SPEECH>

<SPEAKER>HAMLET</SPEAKER>

<REFERENCE>ACT 1, SCENE 2, SECTION 2</REFERENCE>

<LINES>To be, or not to be: that is the question:

Whether 'tis nobler in the mind to suffer

The slings and arrows of outrageous fortune,

Or to take arms against a sea of troubles,

And by opposing end them? To die: to sleep;

No more; and by a sleep to say we end

The heart-ache and the thousand natural shocks

That flesh is heir to, 'tis a consummation

Devoutly to be wish'd. To die, to sleep;

To sleep: perchance to dream: ay, there's the rub;

For in that sleep of death what dreams may come

When we have shuffled off this mortal coil,

Must give us pause: there's the respect

That makes calamity of so long life;

For who would bear the whips and scorns of time,

The oppressor's wrong, the proud man's contumely,

The pangs of despised love, the law's delay,

The insolence of office and the spurns

That patient merit of the unworthy takes,

When he himself might his quietus make

With a bare bodkin? who would fardels bear,

To grunt and sweat under a weary life,

But that the dread of something after death,

The undiscover'd country from whose bourn

No traveller returns, puzzles the will

And makes us rather bear those ills we have

Than fly to others that we know not of?

Thus conscience does make cowards of us all;

And thus the native hue of resolution

Is sicklied o'er with the pale cast of thought,

And enterprises of great pith and moment

With this regard their currents turn awry,

And lose the name of action.--Soft you now!

The fair Ophelia! Nymph, in thy orisons

Be all my sins remember'd.</LINES>

</SPEECH>

<SPEECH>

<REFERENCE>ACT 1, SCENE 3, SECTION 3</REFERENCE>

<SPEAKER>HAMLET</SPEAKER>

<LINES>To be, or not to be: that is the question:

Whether 'tis nobler in the mind to suffer

The slings and arrows of outrageous fortune,

Or to take arms against a sea of troubles,

And by opposing end them? To die: to sleep;

No more; and by a sleep to say we end

The heart-ache and the thousand natural shocks

That flesh is heir to, 'tis a consummation

Devoutly to be wish'd. To die, to sleep;

To sleep: perchance to dream: ay, there's the rub;

For in that sleep of death what dreams may come

When we have shuffled off this mortal coil,

Must give us pause: there's the respect

That makes calamity of so long life;

For who would bear the whips and scorns of time,

The oppressor's wrong, the proud man's contumely,

The pangs of despised love, the law's delay,

The insolence of office and the spurns

That patient merit of the unworthy takes,

When he himself might his quietus make

With a bare bodkin? who would fardels bear,

To grunt and sweat under a weary life,

But that the dread of something after death,

The undiscover'd country from whose bourn

No traveller returns, puzzles the will

And makes us rather bear those ills we have

Than fly to others that we know not of?

Thus conscience does make cowards of us all;

And thus the native hue of resolution

Is sicklied o'er with the pale cast of thought,

And enterprises of great pith and moment

With this regard their currents turn awry,

And lose the name of action.--Soft you now!

The fair Ophelia! Nymph, in thy orisons

Be all my sins remember'd.</LINES>

</SPEECH>

<SPEECH>

<REFERENCE>ACT 1, SCENE 4, SECTION 3</REFERENCE>

<SPEAKER>HAMLET</SPEAKER>

<LINES>To be, or not to be: that is the question:

Whether 'tis nobler in the mind to suffer

The slings and arrows of outrageous fortune,

Or to take arms against a sea of troubles,

And by opposing end them? To die: to sleep;

No more; and by a sleep to say we end

The heart-ache and the thousand natural shocks

That flesh is heir to, 'tis a consummation

Devoutly to be wish'd. To die, to sleep;

To sleep: perchance to dream: ay, there's the rub;

For in that sleep of death what dreams may come

When we have shuffled off this mortal coil,

Must give us pause: there's the respect

That makes calamity of so long life;

For who would bear the whips and scorns of time,

The oppressor's wrong, the proud man's contumely,

The pangs of despised love, the law's delay,

The insolence of office and the spurns

That patient merit of the unworthy takes,

When he himself might his quietus make

With a bare bodkin? who would fardels bear,

To grunt and sweat under a weary life,

But that the dread of something after death,

The undiscover'd country from whose bourn

No traveller returns, puzzles the will

And makes us rather bear those ills we have

Than fly to others that we know not of?

Thus conscience does make cowards of us all;

And thus the native hue of resolution

Is sicklied o'er with the pale cast of thought,

And enterprises of great pith and moment

With this regard their currents turn awry,

And lose the name of action.--Soft you now!

The fair Ophelia! Nymph, in thy orisons

Be all my sins remember'd.</LINES>

</SPEECH>

</SCENE>

</PLAY>

Open in new window

0
Comment
Question by:fsyed
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
sh0e earned 450 total points
Comment Utility
You could just parse it as a string.
string SAXRETURN = "ACT 1, SCENE 1, SECTION 1";

string actscene = SAXRETURN.substring(0, SAXRETURN.lastIndexOf(','));
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 50 total points
Comment Utility
If format of the string is always the same, you can take a couple of approaches.  One is to find the index of the first comma (,) with the string reversed and then take left of the original string for length of the string minus the index.  Or you can find the first index of comma (,) and then search for index of comma again using the first index as the starting point.

Either case it is possible to answer your question.
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 50 total points
Comment Utility
Lol or easily use the lastIndexOf function.  Forgot about that option, so there is at least 3.
0
 

Author Closing Comment

by:fsyed
Comment Utility
Thanks so much for such a quick response!   I didn't realize that the solution would be that simple, and take so little code.  Both of you have been a big help, especially "sh0e.  :-)

Take care.
Sincerely;
Fayyaz
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
wordlen challenge 3 46
java set up 1 44
Detect file exist or not 3 74
XSLT: how to increment number value if attribute present 4 24
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now