0

I have a string that alternates between text and chapter marks. I'd like to have it in a key-value-array where the key is the chapter name and the value is the chapter content. The text looks like this:

<chapter name="First chapter" />
Lorem ipsum dolor sit amet, consetetur sadipscing elitr.
<chapter name="Second chapter" />
Sed diam nonumy eirmod tempor invidunt ut labore et.
<chapter name="Third chapter" />
Dolore magna aliquyam erat, sed diam voluptua.

The resulting array is supposed to look like this:

[
  {"First chapter", "Lorem ipsum dolor sit amet, consetetur sadipscing elitr."},
  {"Second chapter", "Sed diam nonumy eirmod tempor invidunt ut labore et."},
  {"Third chapter", "Dolore magna aliquyam erat, sed diam voluptua."}
]

How can I do this?

eric
  • 2,447
  • 18
  • 24
Socrates
  • 7,512
  • 19
  • 57
  • 100

2 Answers2

0

You can use regular expression to locate subject and content. Your case is very suitable for that. The link below has a summary for regex in java. http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

0

As suggested by @devd with this posting, the solution to the above case is XPath. There is an example here.

Socrates
  • 7,512
  • 19
  • 57
  • 100