1

I have an XML file, and i want to get the text from one tag, but if that text contain another tag to ignore it. For example :

<?xml version="1.0"?>
<entries>
  <entry accente="B" diacritice="B">
    <sense class="0" value="B">
      <definition>
        <RegDef>Hello  <i>world.</i> Today is Saturday.</RegDef>
      </definition>
    </sense>
  </entry>
 </entries>

the output should be : "Hello world. Today is Saturday.

What is the best method to do this?

Diana Condurache
  • 159
  • 1
  • 11
  • Not regex: http://stackoverflow.com/a/1732454/3580294 – awksp May 24 '14 at 08:57
  • 1
    When you parse with XPath, you specify what type of result you want. If you don't specify any particular type, you get a `String`, which in this case happens to be exactly what you want. So look into `XPath`. If you can't work out how to do it, post a comment and I will post a proper answer. – Dawood ibn Kareem May 24 '14 at 08:59
  • 1
    @Braj Maybe the second half of this question is a duplicate of that one - but that question doesn't deal with pulling out the required tag to start with. – Dawood ibn Kareem May 24 '14 at 09:06

2 Answers2

3

Thank you, @David Wallace.

String expression = "/entries/entry/sense/definition/RegDef";
System.out.println(expression);
String RegDef = xPath.compile(expression).evaluate(xmlDocument);
System.out.println(RegDef);

This does exactly what I want.

Diana Condurache
  • 159
  • 1
  • 11
0

i believe, Ignoring tag in xml couldn't be done. so the best solution to your problem is,

1- read the contents of the XML into a String

2- parse the String and remove all unwanted tags & characters.

3- write the String back into the file. or create a new file if you can't modify the original

4- parse the modified/new file.

Hope this helps.