Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.List;

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());
        }
    }
}