user1646537
user1646537

Reputation: 87

java xml parsing between tags

What im trying to do is parse xml through java. and i only want a snippet of text from each tag for example.

xml example

<data>\nSome Text :\n\MY Spectre around me night and day. Some More: Like a wild beast
guards my way.</data>

<data>\nSome Text :\n\Cruelty has a human heart. Some More: And Jealousy a human face
</data>

so far i have this

NodeList ageList = firstItemElement.getElementsByTagName("data");
Element ageElement =(Element)ageList.item(0);
NodeList textAgeList = ageElement.getChildNodes();
out.write("Data : " + ((Node)textAgeList.item(0)).getNodeValue().trim());   

im trying to just get the "Some More:....." part i dont want the whole tag also im trying to get rid of all the \n

Upvotes: 0

Views: 489

Answers (2)

Lukas Eder
Lukas Eder

Reputation: 221106

If you're not restricted to the standard DOM API, you could try to use jOOX, which wraps standard DOM. Your example would then translate to:

// Use jOOX's jquery-like API to find elements and their text content
for (String string : $(firstItemElement).find("data").texts()) {

  // Use standard String methods to replace content
  System.out.println(string.replace("\\n", ""));
}

Upvotes: 1

jpstrube
jpstrube

Reputation: 795

I would take all of the element text and use regular expressions to capture the relevant parts.

Upvotes: 0

Related Questions