Java HTML / XML How to - XML getting text around a tag








Question

We would like to know how to xML getting text around a tag.

Answer

import java.util.List;
//from   w  w w.j a  v  a 2  s .c  o  m
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class Main {

    public static Document getDocument(final String xmlFileName) {
        Document document = null;
        SAXReader reader = new SAXReader();
        try {
            document = reader.read(xmlFileName);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return document;
    }
    public static void main(String[] args) {
        String xmlFileName = "data.xml";
        String xPath = "//article/body/p";
        Document document = getDocument(xmlFileName);
        List<Node> nodes = document.selectNodes(xPath);
        for (Node node : nodes) {
            String nodeXml = node.asXML();
            System.out.println("Left  >> " + nodeXml.substring(3, nodeXml.indexOf("<ref")).trim());
            System.out.println("Right  >> " + nodeXml.substring(nodeXml.indexOf("</ref>") + 6, nodeXml.length() - 4).trim());
        }
    }
}