Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"[Fatal Error] :1:120: The processing instruction target matching "[xX][mM][lL]" is not allowed." [duplicate]

Tags:

Here's a tough one for you.

I am working on a class project where I create a peer-to-peer chat program and I have this problem:

When I open up a chat window, no problems. When I open up a second window and try and login to the chat, I get this error:

**[Fatal Error] :1:120: The processing instruction target matching "[xX][mM][lL]" is not allowed. org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.         at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:249)         at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)         at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)         at chatter2.Chatter.process(Chatter.java:240)         at chatter2.Chatter.run(Chatter.java:222)         at java.lang.Thread.run(Thread.java:680)** 

I am pretty sure it has something to do with how my code is creating the XML for participants.

Here is all the code I have written:

    /*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */  /*  * NewJFrame.java  *  * Created on Nov 10, 2010, 2:11:39 PM  */ package chatter2;  import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringBufferInputStream; import java.io.StringReader; import java.net.Socket; import java.util.LinkedList; import java.util.List; import javax.swing.DefaultListModel; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource;  /**  *  * @author ericrea  */ public class Chatter extends javax.swing.JFrame implements Runnable {      PrintWriter out = null;     BufferedReader in = null;     boolean running = true;     String partName = "";     String chatHist = "";      /** Creates new form NewJFrame */     public Chatter() {         initComponents();         Server server = new Server();         server.start();     }      /** This method is called from within the constructor to      * initialize the form.      * WARNING: Do NOT modify this code. The content of this method is      * always regenerated by the Form Editor.      */     @SuppressWarnings("unchecked")     // <editor-fold defaultstate="collapsed" desc="Generated Code">                               private void initComponents() {          jPanel1 = new javax.swing.JPanel();         msgText = new javax.swing.JTextArea();         send = new javax.swing.JButton();         jPanel2 = new javax.swing.JPanel();         chatText = new javax.swing.JTextArea();         jPanel3 = new javax.swing.JPanel();         userName = new javax.swing.JTextField();         IPaddress = new javax.swing.JTextField();         PortField = new javax.swing.JTextField();         Login = new javax.swing.JButton();         jButton1 = new javax.swing.JButton();         jPanel4 = new javax.swing.JPanel();         chatMembers = new javax.swing.JList();          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);         setMinimumSize(new java.awt.Dimension(550, 550));          jPanel1.setLayout(new java.awt.GridLayout(1, 0));          msgText.setColumns(20);         msgText.setRows(5);         msgText.setPreferredSize(new java.awt.Dimension(240, 24));         msgText.setRequestFocusEnabled(false);         jPanel1.add(msgText);          send.setText("Send");         send.setPreferredSize(new java.awt.Dimension(100, 29));         send.setRolloverEnabled(true);         send.setSelected(true);         send.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 sendActionPerformed(evt);             }         });         jPanel1.add(send);          getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);          jPanel2.setLayout(new java.awt.GridLayout(1, 0));          chatText.setBackground(new java.awt.Color(0, 255, 204));         chatText.setColumns(20);         chatText.setRows(5);         jPanel2.add(chatText);          getContentPane().add(jPanel2, java.awt.BorderLayout.LINE_END);          jPanel3.setLayout(new java.awt.GridLayout(1, 0));          userName.setText("UserName");         jPanel3.add(userName);          IPaddress.setText("127.0.0.1");         IPaddress.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 IPaddressActionPerformed(evt);             }         });         jPanel3.add(IPaddress);          PortField.setText("44640");         jPanel3.add(PortField);          Login.setText("Login");         Login.addActionListener(new java.awt.event.ActionListener() {             public void actionPerformed(java.awt.event.ActionEvent evt) {                 LoginActionPerformed(evt);             }         });         jPanel3.add(Login);          jButton1.setText("Logout");         jPanel3.add(jButton1);          getContentPane().add(jPanel3, java.awt.BorderLayout.PAGE_START);          chatMembers.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));         chatMembers.setModel(new javax.swing.AbstractListModel() {             String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };             public int getSize() { return strings.length; }             public Object getElementAt(int i) { return strings[i]; }         });         chatMembers.setPreferredSize(new java.awt.Dimension(80, 87));         jPanel4.add(chatMembers);          getContentPane().add(jPanel4, java.awt.BorderLayout.LINE_START);          pack();     }// </editor-fold>                              private void LoginActionPerformed(java.awt.event.ActionEvent evt) {                                                try {             Socket s = new Socket(IPaddress.getText(), Integer.parseInt(PortField.getText()));             out = new PrintWriter(s.getOutputStream());             in = new BufferedReader(new InputStreamReader(s.getInputStream()));             new Thread(this).start();             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();             DocumentBuilder docBuilder = factory.newDocumentBuilder();             Document doc = docBuilder.newDocument();             Element root = doc.createElement("login");             doc.appendChild(root);             root.appendChild(doc.createTextNode(userName.getText()));              TransformerFactory fact = TransformerFactory.newInstance();             Transformer trans = fact.newTransformer();             DOMSource source = new DOMSource(doc);             StreamResult sResult = new StreamResult(out);             trans.transform(source, sResult);             out.println("\n");              out.flush();         } catch (Exception e) {         }     }                                           private void IPaddressActionPerformed(java.awt.event.ActionEvent evt) {                                                   // TODO add your handling code here:     }                                               private void sendActionPerformed(java.awt.event.ActionEvent evt) {                                           }                                          /**      * @param args the command line arguments      */     public static void main(String args[]) {         java.awt.EventQueue.invokeLater(new Runnable() {              public void run() {                 new Chatter().setVisible(true);             }         });     }     // Variables declaration - do not modify                          private javax.swing.JTextField IPaddress;     private javax.swing.JButton Login;     private javax.swing.JTextField PortField;     private javax.swing.JList chatMembers;     private javax.swing.JTextArea chatText;     private javax.swing.JButton jButton1;     private javax.swing.JPanel jPanel1;     private javax.swing.JPanel jPanel2;     private javax.swing.JPanel jPanel3;     private javax.swing.JPanel jPanel4;     private javax.swing.JTextArea msgText;     private javax.swing.JButton send;     private javax.swing.JTextField userName;     // End of variables declaration                         public void run() {          String buffer = "";         while (running) {             try {                  String line = in.readLine();                 System.out.println(line);                  if (line.equals("")) {                      process(buffer);                 } else {                      buffer = buffer + line;                 }             } catch (Exception e) {             }          }     }      public void process(String buffer) {          try {              System.out.println("buffer in process is " + buffer);             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();             DocumentBuilder docBuilder = factory.newDocumentBuilder();             Document doc = docBuilder.parse(new StringBufferInputStream(buffer)); //new InputSource(new StringReader(buffer))             Element root = doc.getDocumentElement();                if (root.getNodeName().equals("message")) {                 chatHist = chatHist + root.getTextContent() + "\n";                 newMessage();              }             else if (root.getNodeName().equals("participants")) {                 DefaultListModel partNames = new DefaultListModel();                  for(int i = 0; i < root.getChildNodes().getLength(); i++){                      //partName = partName + root.getChildNodes().item(i).getTextContent() + "/n";                      partNames.addElement(root.getChildNodes().item(i).getTextContent());                  }                 chatMembers.setModel(partNames);              }          } catch (Exception e) {e.printStackTrace();         }          }      public void cleanStop() {     }      public void newMessage() {         chatText.setText(chatHist);     } }       /*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */  package chatter2;   import java.io.*; import java.net.*; import java.util.LinkedList; import java.util.List; import org.w3c.dom.Document;   /**  *  * @author ericrea  */ public class Server extends Thread {      private ServerSocket ss = null;     private List<Participant> parts = new LinkedList<Participant>();      public Server(){         try{         ss = new ServerSocket(44640);         }catch(Exception e){e.printStackTrace();         }      }      @Override     public void run() {        //add this into a while loop             while (true){                  try{             Socket s = ss.accept();             Participant p = new Participant(this, s);             p.start();             getParts().add(p);             }             catch(Exception e){             e.printStackTrace();}      //            System.out.println(" Got a client socket connection"); //            PrintWriter out =  new PrintWriter(s.getOutputStream()); //            BufferedReader in =  new BufferedReader(new InputStreamReader(s.getInputStream())); //            out.println("hey there, want to chat"); //            out.flush(); //            String line = in.readLine(); //            System.out.println("Client said: " + line);           }      }     public void message(Document doc){         for (Participant p: getParts()){             p.newMessage(doc);         }     }      public void newParticipants(){        int counter = 1;         for(Participant p: getParts()){             //System.out.println(counter + " time through the loop");             counter++;             p.newParticipant();         }       }      public void cleanStop(){      }     public void logout(Participant p){         parts.remove(p);         newParticipants();     }      /**      * @return the ss      */     public ServerSocket getSs() {         return ss;     }      /**      * @param ss the ss to set      */     public void setSs(ServerSocket ss) {         this.ss = ss;     }      /**      * @return the parts      */     public List<Participant> getParts() {         return parts;     }      /**      * @param parts the parts to set      */     public void setParts(List<Participant> parts) {         this.parts = parts;     }  }       /*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ package chatter2;  import java.io.*; import java.util.*; import java.net.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource;  /**  *  * @author ericrea  */ public class Participant extends Thread {      Server server = null;     Socket client = null;     PrintWriter out = null;     BufferedReader in = null;     boolean running = true;     private String partName = null;      public Participant(Server server, Socket client) throws IOException {         this.client = client;         this.server = server;         out = new PrintWriter(client.getOutputStream());         in = new BufferedReader(new InputStreamReader(client.getInputStream()));     }      //run and actONMessage will be in the chatter class as well     @Override     public void run() {               String buffer = "";             while (running) {                 try {                     String line = in.readLine();                      if (line.equals("")) {                          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();                         DocumentBuilder docBuilder = factory.newDocumentBuilder();                         Document doc = docBuilder.parse(new InputSource(new StringReader(buffer)));                         actOnMessage(doc);                     } else {                         buffer = buffer + line;                     }                  } catch (Exception e) {                     e.printStackTrace();                 }             }      }      public void actOnMessage(Document doc) {          Element root = doc.getDocumentElement();           if (root.getNodeName().equals("login")) {             setPartName(root.getTextContent());              this.login();         } else if (root.getNodeName().equals("message")) {             message(doc);         } else if (root.getNodeName().equals("logout")) {             this.logout();         }      }      public void message(Document doc) {         server.message(doc);      }      public void login() {         server.newParticipants();     }      public void logout() {         server.logout(this);     }      public void newMessage(Document doc) {         out.println(/*String version of the xml*/);     }      public void newParticipant() {         try {              List<Participant> partList = server.getParts();              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();             DocumentBuilder docBuilder = factory.newDocumentBuilder();             Document doc = docBuilder.newDocument();             Element root = doc.createElement("participants");             doc.appendChild(root);              for (Participant k : partList) {                 Element root1 = doc.createElement("participant");                 root.appendChild(root1);                 root1.appendChild(doc.createTextNode(k.getPartName()));              }              TransformerFactory fact = TransformerFactory.newInstance();             Transformer trans = fact.newTransformer();             DOMSource source = new DOMSource(doc);             StreamResult sResult = new StreamResult(out);             trans.transform(source, sResult);             out.println("\n");              out.flush();         } catch (Exception e) {         }     }      public void cleanStop() {     }      public void getParticipantName() {     }      /**      * @return the partName      */     public String getPartName() {         return partName;     }      /**      * @param partName the partName to set      */     public void setPartName(String partName) {         this.partName = partName;     } } 
like image 471
novicePrgrmr Avatar asked Nov 21 '10 01:11

novicePrgrmr


1 Answers

The problem is that you have more than one XML header or noise before one.

The typical start of an XML doc...

<?xml version='1.0'?> 

looks like a PI, but isn't. If you have an extra, or if you have anything other than a BOM before one, that's the error you'll get.

like image 104
bmargulies Avatar answered Dec 05 '22 03:12

bmargulies