0

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

2
  • 1
    What specifically is not working? Commented Sep 27, 2012 at 11:06
  • i edited it sorry i wasnt clear i dont want the whole tag just from "Some More:" Commented Sep 27, 2012 at 11:28

2 Answers 2

1

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", ""));
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Look for the package java.util.regex in the API doc. There´s all you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.