|
| 1 | +/* |
| 2 | + * To change this template, choose Tools | Templates |
| 3 | + * and open the template in the editor. |
| 4 | + */ |
| 5 | +/** |
| 6 | + * |
| 7 | + * Date Author Changes Sep 10, 2013 Kasun Perera Created |
| 8 | + * |
| 9 | + */ |
| 10 | +package org.dbpedia.kasun.wikiquery; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * TODO- describe the purpose of the class |
| 15 | + * |
| 16 | + */ |
| 17 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 18 | +import javax.xml.parsers.DocumentBuilder; |
| 19 | +import org.w3c.dom.Document; |
| 20 | +import org.w3c.dom.NodeList; |
| 21 | +import org.w3c.dom.Node; |
| 22 | +import org.w3c.dom.Element; |
| 23 | +import java.io.File; |
| 24 | +import java.io.UnsupportedEncodingException; |
| 25 | +import java.net.URLEncoder; |
| 26 | + |
| 27 | +public class ReadXMLFile |
| 28 | +{ |
| 29 | + |
| 30 | + public static void ReadFile( String filename ) |
| 31 | + { |
| 32 | + //public static void ReadFile(File fXmlFile) { |
| 33 | + try |
| 34 | + { |
| 35 | + |
| 36 | + File fXmlFile = new File( filename ); |
| 37 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 38 | + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 39 | + Document doc = dBuilder.parse( fXmlFile ); |
| 40 | + |
| 41 | + //optional, but recommended |
| 42 | + //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work |
| 43 | + doc.getDocumentElement().normalize(); |
| 44 | + |
| 45 | + System.out.println( "Root element :" + doc.getDocumentElement().getNodeName() ); |
| 46 | + |
| 47 | + NodeList nList = doc.getElementsByTagName( "rev" ); |
| 48 | + |
| 49 | + System.out.println( "----------------------------" ); |
| 50 | + |
| 51 | + for ( int temp = 0; temp < nList.getLength(); temp++ ) |
| 52 | + { |
| 53 | + |
| 54 | + Node nNode = nList.item( temp ); |
| 55 | + |
| 56 | + System.out.println( "Current Element :" + nNode.getNodeName() ); |
| 57 | + |
| 58 | + if ( nNode.getNodeType() == Node.ELEMENT_NODE ) |
| 59 | + { |
| 60 | + |
| 61 | + |
| 62 | + Element eElement = (Element) nNode; |
| 63 | + |
| 64 | + System.out.println( "Revision22222 id : " + eElement.getAttribute( "revid" ) ); |
| 65 | +// System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent()); |
| 66 | +// System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent()); |
| 67 | +// System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent()); |
| 68 | +// System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent()); |
| 69 | + |
| 70 | + } |
| 71 | + } |
| 72 | + } catch ( Exception e ) |
| 73 | + { |
| 74 | + e.printStackTrace(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static int ReadFile( Document doc ,String urlParameters, String url) throws UnsupportedEncodingException |
| 79 | + { |
| 80 | + |
| 81 | + int numberOfRevisions=0; |
| 82 | + //public static void ReadFile(File fXmlFile) { |
| 83 | + try |
| 84 | + { |
| 85 | + doc.getDocumentElement().normalize(); |
| 86 | + |
| 87 | + // System.out.println( "Root element :" + doc.getDocumentElement().getNodeName() ); |
| 88 | + |
| 89 | + NodeList continueNodeList = doc.getElementsByTagName( "revisions" ); |
| 90 | + if ( continueNodeList.getLength() > 0 ) |
| 91 | + { |
| 92 | + Node continueNode = continueNodeList.item( 0 ); |
| 93 | + |
| 94 | + Element continueElement = (Element) continueNode; |
| 95 | + // String urlParameters = "fName=" + URLEncoder.encode( "???", "UTF-8" ) + "&lName=" + URLEncoder.encode( "???", "UTF-8" ); |
| 96 | + // String url = "http://en.wikipedia.org/w/api.php?action=query&format=xml&prop=revisions&titles=Mother&rvlimit=max&rvstart=20130604000000&rvcontinue="+continueElement.getAttribute( "rvcontinue" ); |
| 97 | + |
| 98 | + // System.out.println("Calling recursive function using rivision Id "+ continueElement.getAttribute( "rvcontinue" )); |
| 99 | + numberOfRevisions=ReadFile(RevisionHistory.excutePost( url+ "&rvcontinue="+continueElement.getAttribute( "rvcontinue" ), urlParameters ),urlParameters, url ); |
| 100 | + |
| 101 | + // System.out.println( "Continue revision Id : " + continueElement.getAttribute( "rvcontinue" ) ); |
| 102 | + } |
| 103 | + |
| 104 | + NodeList nList = doc.getElementsByTagName( "rev" ); |
| 105 | + |
| 106 | + // System.out.println( "number of nodes" + nList.getLength()); |
| 107 | +/* |
| 108 | + for ( int temp = 0; temp < nList.getLength(); temp++ ) |
| 109 | + { |
| 110 | +
|
| 111 | + Node nNode = nList.item( temp ); |
| 112 | +
|
| 113 | + // System.out.println( "\nCurrent Element :" + nNode.getNodeName() + " count: " + temp ); |
| 114 | +
|
| 115 | + if ( nNode.getNodeType() == Node.ELEMENT_NODE ) |
| 116 | + { |
| 117 | +
|
| 118 | + Element eElement = (Element) nNode; |
| 119 | +
|
| 120 | + System.out.println( "Revision id : " + eElement.getAttribute( "revid" ) ); |
| 121 | +
|
| 122 | + } |
| 123 | + } |
| 124 | + */ |
| 125 | + |
| 126 | + return numberOfRevisions+ nList.getLength(); |
| 127 | + } catch ( Exception e ) |
| 128 | + { |
| 129 | + e.printStackTrace(); |
| 130 | + return 0; |
| 131 | + |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments