Possible Duplicate:
Java:XML Parser
I have a XML file, in which i want to get the text only within the specified tags(lets say, only the text between "<HERE> ... </HERE>
. Each file have multiple 开发者_开发技巧"<HERE>"
blocks. How can i get that?
I was using this for normal text files:
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
..
}
I want to be able to get only the multiple blocks of text inside the tag.
I would type a long response about XML parsing in Java, but one of the best quick reads on it which I cannot beat is this Dzone article:
http://refcardz.dzone.com/refcardz/using-xml-java
Explains all you need to know in just a few pages. Definitely worth a read.
While there's better answers, without the fundamentals you'll not appreciate them.
Learn SAX parsing. Basically the parser will call your class when entering and exiting tags. You just need to keep track of the depth, or where you are in the document, check the tag names, and capture the text you want in a StringBuilder buffer. After the parser is complete, you do a toString()
on the buffer and get your combined text.
Later on, learn DOM parsing. Then learn XPath. However, without learning how to parse XML using an XML parser you will burn through way too much time and brainpower attempting to solve a problem badly. Building a parser from scratch isn't impossible; however, it is stealing away from your time solving the problem at hand (and odds are you don't know enough about XML yet to parse it correctly).
精彩评论