In this program, you will see complete steps to extract XML file content and metadata by using XMLParser.
Sample File

Complete Example
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.tika.exception.TikaException; import org.apache.tika.metadata.Metadata; import org.apache.tika.parser.ParseContext; import org.apache.tika.parser.xml.XMLParser; import org.apache.tika.sax.BodyContentHandler; import org.xml.sax.SAXException; public class TikaXmlParserExample { public static void main(final String[] args) throws IOException, SAXException, TikaException { // detecting the file type BodyContentHandler handler = new BodyContentHandler(); Metadata metadata = new Metadata(); FileInputStream inputstream = new FileInputStream(new File("C:\\Users\\Saurabh Gupta\\Desktop\\TIKA\\EmployeeData.xml")); ParseContext pcontext = new ParseContext(); // Xml parser XMLParser xmlparser = new XMLParser(); xmlparser.parse(inputstream, handler, metadata, pcontext); System.out.println("Contents of the XML document:" + handler.toString()); System.out.println("Metadata of the XML document:"); String[] metadataNames = metadata.names(); for (String name : metadataNames) { System.out.println(name + ": " + metadata.get(name)); } } }
Output
Contents of the XML document:
Saurabh
Gupta
13-Jun-1980
Gaurav
Kumar
12-Dec-1987
Rajesh
Gupta
10-Jan-1999
Raghvendra
Prasad
14-Mar-1985
Rahul
Jain
11-May-1981
Metadata of the XML document:
Content-Type: application/xml
You must log in to post a comment.