Using DOM4J to parse the downloaded XML file, the Java program is as follows:
Packagegao.map.preprocess;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStreamWriter;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List;ImportOrg.dom4j.Attribute;Importorg.dom4j.Document;Importorg.dom4j.DocumentException;Importorg.dom4j.Element;ImportOrg.dom4j.io.SAXReader;/*** Processing raw data downloaded from OpenStreetMap, outputting extracted data as TXT file *@authorAdministrator **/ Public classOpenStreetMap {//traverse all nodes under the current node@SuppressWarnings ("Unchecked") Public Static voidListnodes (Element node) {System.out.println ("Name of the current node:" +node.getname ()); //first gets all the attribute nodes of the current nodelist<attribute> list =node.attributes (); //traversing attribute nodes for(Attribute attribute:list) {System.out.println ("Attribute" +attribute.getname () + ":" +Attribute.getvalue ()); } //if the contents of the current node are not empty, the output if(! (Node.gettexttrim (). Equals ("")) {System.out.println (Node.getname () ))+ ":" +Node.gettext ()); } //iterates all child nodes below the current node at the same time//use recursionIterator<element> Iterator =Node.elementiterator (); while(Iterator.hasnext ()) {Element e=Iterator.next (); Listnodes (e); }} @SuppressWarnings ("Unchecked") Public Static voidMain (string[] args)throwsIOException, documentexception {//Point InformationFile Pointfile =NewFile ("d:\\baiduyundownload\\ New York rental data \\15 year 1-6 yellow car-green car data \\yellow_tripdata_2015-06.csv\\Point.txt"); //Arc InformationFile Arcfile =NewFile ("d:\\baiduyundownload\\ New York rental data \\15 year 1-6 yellow car-green car data \\yellow_tripdata_2015-06.csv\\Arc.txt"); FileOutputStream Fospoint=NewFileOutputStream (Pointfile); FileOutputStream Fosarc=NewFileOutputStream (Arcfile); OutputStreamWriter Oswpoint=NewOutputStreamWriter (Fospoint); OutputStreamWriter Oswarc=NewOutputStreamWriter (FOSARC); BufferedWriter Bwpoint=NewBufferedWriter (Oswpoint); BufferedWriter Bwarc=NewBufferedWriter (OSWARC); Saxreader Reader=NewSaxreader (); //the original map data to readString Path = "d:\\baiduyundownload\\ New York rental data \\15 year 1-6 yellow car-green car data \\yellow_tripdata_2015-06.csv\\map"; Document Document= Reader.read (NewFile (path)); //get root node element object OSMElement root =document.getrootelement (); //Traverse all child nodes under the OSMIterator<element> Iterator =Root.elementiterator (); while(Iterator.hasnext ()) {Element e=Iterator.next ();//if (E.getname (). Equals ("Node") | | E.getname (). Equals ("the"))//Listnodes (e); //Output Point Information if(E.getname (). Equals ("Node") ) {StringBuilder sb=NewStringBuilder (); //first gets all the attribute nodes of the current nodelist<attribute> list =e.attributes (); //traversing attribute nodes for(Attribute attribute:list) {if(Attribute.getname (). Equals ("id")) Sb.append (Attribute.getvalue ()+" "); if(Attribute.getname (). Equals ("Lat")) Sb.append (Attribute.getvalue ()+" "); if(Attribute.getname (). Equals ("Lon") ) Sb.append (Attribute.getvalue ()); } bwpoint.write (Sb.tostring ()+ "\ r \ n"); Bwpoint.flush (); System.out.println (Sb.tostring ()); }Else if(E.getname (). Equals ("the")) {//Output ARC InformationStringBuilder SB =NewStringBuilder (); String s= ""; //first gets all the attribute nodes of the current nodelist<attribute> list =e.attributes (); //traversing attribute nodes for(Attribute attribute:list) {if(Attribute.getname (). Equals ("id")) S+ = Attribute.getvalue () + ""; if(Attribute.getname (). Equals ("Version")) S+ = Attribute.getvalue () + ""; } //Traversing child nodesIterator<element> iter =E.elementiterator (); while(Iter.hasnext ()) {element element=Iter.next (); //first gets all the attribute nodes of the current nodeList<attribute> List1 =element.attributes (); //traversing attribute nodes for(Attribute attribute:list1) {if(Attribute.getname (). Equals ("ref")) Sb.append (s+ attribute.getvalue () + "" + "\ r \ n"); Else if(Attribute.getname (). Equals ("K")) Sb.append (s+ "" +attribute.getvalue () + "\ r \ n"); }} bwarc.write (Sb.tostring ()); Bwarc.flush (); System.out.print (Sb.tostring ()); }} bwpoint.close (); Oswpoint.close (); Fospoint.close (); Bwarc.close (); Oswarc.close (); Fosarc.close (); System.out.println ("The output is complete!" "); }}
Using Java to parse map data downloaded from OpenStreetMap