Posted on Leave a comment

MVC MVP

According to both MVC and MVP the presentation layer consists of view objects, and application logic consists of controller objects (we will use “controller” name instead of “presenter” in MVP). For each view object a corresponding controller exists and vice versa. And although MVC and MVP are based on a common 3-tier principle: views process only presentation needs and controllers handle application logic, these patterns have two major differences:

  • In MVC controllers receive and process user input, but in MVP views receive user input and then merely delegate processing to the corresponding controllers. That is why MVP pattern better fits modern UI environments (Windows/Web forms) where view classes themselves handle user gestures.
  • In MVC controllers affect their views by changing the intermediate presentation model, which the views are subscribed to (by observer pattern). This makes views pure observers without direct access to them. MVP on the other hand violates this “pure observer” rule by providing a direct link from a controller to its view. This makes MVP more handy as compared to MVC.

Continue reading MVC MVP

Posted on Leave a comment

Parsing xml string in Java

JDOM

String xml = "<message>HELLO!</message>";
org.jdom.input.SAXBuilder saxBuilder = new SAXBuilder();
try {
    org.jdom.Document doc = saxBuilder.build(new StringReader(xml));
    String message = doc.getRootElement().getText();
    System.out.println(message);
} catch (JDOMException e) {
    // handle JDOMException
} catch (IOException e) {
    // handle IOException
}

Xerces

String xml = "<message>HELLO!</message>";
DOMParser parser = new DOMParser();
try {
    parser.parse(new InputSource(new java.io.StringReader(xml)));
    Document doc = parser.getDocument();
    String message = doc.getDocumentElement().getTextContent();
    System.out.println(message);
} catch (SAXException e) {
    // handle SAXException 
} catch (IOException e) {
    // handle IOException 
}

JAXP

String xml = "<message>HELLO!</message>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
    db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    try {
        Document doc = db.parse(is);
        String message = doc.getDocumentElement().getTextContent();
        System.out.println(message);
    } catch (SAXException e) {
        // handle SAXException
    } catch (IOException e) {
        // handle IOException
    }
} catch (ParserConfigurationException e1) {
    // handle ParserConfigurationException
}