简体   繁体   中英

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

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", ""));
}

我将使用所有元素文本并使用正则表达式来捕获相关部分。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM