Wednesday, 12 October 2016

XML : Control of xml file with Java Standard Edition. I need help modifying Java code

// I'm new in Java and i found this code online that I'm trynna modify. It's a chat app and this class stores the conversation in an xml file then displays it on a jTable when the user clicks a button. i want to modify it so that it only displays a conversation between a selected recipient on a jlist and not the whole conversation in the xml file. but I'm new in Java so i don't completely understand the functions. Can someone help explaining so i can understand and modify it? thanks

  public class History {    public String filePath;    public History(String filePath){      this.filePath = filePath;  }    public void addMessage(Message msg, String time){        try {          DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();          DocumentBuilder docBuilder = docFactory.newDocumentBuilder();          Document doc = docBuilder.parse(filePath);            Node data = doc.getFirstChild();            Element message = doc.createElement("message");          Element _sender = doc.createElement("sender");           _sender.setTextContent(msg.sender);          Element _content = doc.createElement("content");          _content.setTextContent(msg.content);          Element _recipient = doc.createElement("recipient");           _recipient.setTextContent(msg.recipient);          Element _time = doc.createElement("time");           _time.setTextContent(time);            message.appendChild(_sender);           message.appendChild(_content);           message.appendChild(_recipient);           message.appendChild(_time);          data.appendChild(message);            TransformerFactory transformerFactory = TransformerFactory.newInstance();          Transformer transformer = transformerFactory.newTransformer();          DOMSource source = new DOMSource(doc);          StreamResult result = new StreamResult(new File(filePath));          transformer.transform(source, result);       }          catch(Exception ex){      System.out.println("Exceptionmodify xml");     }  }    public void FillTable(HistoryFrame frame){        DefaultTableModel model = (DefaultTableModel) frame.jTable1.getModel();        try{          File fXmlFile = new File(filePath);          DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();          DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();          Document doc = dBuilder.parse(fXmlFile);          doc.getDocumentElement().normalize();            NodeList nList = doc.getElementsByTagName("message");            for (int temp = 0; temp < nList.getLength(); temp++) {              Node nNode = nList.item(temp);              if (nNode.getNodeType() == Node.ELEMENT_NODE) {                  Element eElement = (Element) nNode;                  model.addRow(new Object[]{getTagValue("sender", eElement), getTagValue("content", eElement), getTagValue("recipient", eElement), getTagValue("time", eElement)});              }          }      }      catch(Exception ex){          System.out.println("Filling Exception");      }  }    public static String getTagValue(String sTag, Element eElement) {  NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();      Node nValue = (Node) nlList.item(0);  return nValue.getNodeValue();  }      }    

No comments:

Post a Comment