Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. import java.io.IOException;
  2.  
  3. public class XmlParser {
  4.  
  5.     private static final String[] COLUMNS = new String[] { "Last", "First", "Middle", "Title", "Department", "Location",
  6.             "Phone", "Email" };
  7.  
  8.     private static final String[] LINES = new String[] {
  9.             "Amodeo,Paul,A,Professor,Business & Information Technology,Warner Hall,518-255-5384,amodeopa@cobleskill.edu",
  10.             "Yacobucci,Kelly,R,First Year Experience Coordinator,School of Business | Lib Arts & Sci,Frisbie Hall 110,518-255-5460,yacobukr@cobleskill.edu",
  11.             "Fisher,James,D,Visiting Instructor,Business & Information Technology,Warner Hall 101,518-255-5826,fisherjd@cobleskill.edu",
  12.             "Schwarzenegger,Karl,F,Associate Professor,Business & Information Technology,Warner Hall,518-255-5693,schwarkf@cobleskill.edu",
  13.             "Luria,Patricia,L,Adjunct Instructor,Liberal Studies,Frisbie 300,518-255-5760,luriapl@cobleskill.edu",
  14.             "Eldred,Melody,E,Professor,Natural Sciences & Mathematics,Frisbie Hall 101,518-255-5116,eldredme@cobleskill.edu",
  15.             "Begany,Grace,M,Assistant Professor 10 Months,Business & Information Technology,Warner Hall,518-255-5273,beganygm@cobleskill.edu", };
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.         XmlParser parser = new XmlParser();
  19.         StringBuilder xml = new StringBuilder("<Directory>\n");
  20.  
  21.         for (String line : LINES) {
  22.             String record = parser.parse(line);
  23.             xml.append(record).append("\n");
  24.         }
  25.         xml.append("</Directory>");
  26.  
  27.         System.out.println(xml);
  28.     }
  29.  
  30.     private String parse(String commaSeperated) {
  31.         String[] data = commaSeperated.split("\\s*,\\s*");
  32.         StringBuilder xml = new StringBuilder("\t<Faculty>\n");
  33.  
  34.         for (int id = 0; id < data.length; id++) {
  35.             StringBuilder tag = new StringBuilder("<" + COLUMNS[id] + ">");
  36.             xml.append("\t\t").append(tag).append(data[id]).append(tag.insert(1, "/")).append("\n");
  37.         }
  38.         xml.append("\t</Faculty>");
  39.         return xml.toString();
  40.     }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement