Package com.skaringa.javaxml

Examples of com.skaringa.javaxml.ObjectTransformer


    Person fred = new Person(102, "Fred", "Feuerstein", "ff@email.com");

    try {
      // Get an ObjectTransformer.
      // The ObjectTransformer interface offers all needed methods.
      ObjectTransformer trans = ObjectTransformerFactory.getInstance()
          .getImplementation();

      // Serialize the Person object into a string.
      String json = trans.serializeToJsonString(fred);
     
      // Call PHP.
      // The script converts the JSON string into an object, sets the attribute 'firstName' to 'Horst'
      // and converts the object back to JSON string
      InputStream convertedJson = execPHP("$person = json_decode($_ARGV[0]);$person->firstName = 'Horst'; fwrite(STDOUT, json_encode($person));", json);
     
      // Read JSON from PHP back into person
      Person horst = (Person) trans.deserializeFromJson(convertedJson, Person.class);
      convertedJson.close();

      // Check the result.
      if ("Horst".equals(horst.getFirstName())) {
        System.out.println("Horst OK");
View Full Code Here


    Person fred = new Person(101, "Fred", "Feuerstein", "ff@email.com");

    try {
      // Get an ObjectTransformer.
      // The ObjectTransformer interface offers all needed methods.
      ObjectTransformer trans = ObjectTransformerFactory.getInstance()
          .getImplementation();

      // Serialize the Person object into a file.
      FileOutputStream out = new FileOutputStream("fred.js");
      trans.serializeToJson(fred, out);
      out.close();

      // Read a Person's JSON file and create a new Person object
      // from its content.
      FileInputStream in = new FileInputStream("fred.js");
      Person freddy = (Person) trans.deserializeFromJson(in, Person.class);
      in.close();

      // Check the result.
      if (fred.equals(freddy)) {
        System.out.println("OK");
      } else {
        System.out.println("ERROR");
      }
     
      // now deserialize the JSON file into a map instead of a person
      in = new FileInputStream("fred.js");
      Map fredMap = (Map) trans.deserializeFromJson(in);
      in.close();
     
      System.out.println(fredMap);
    } catch (NoImplementationException e) {
      System.err.println(e);
View Full Code Here

    Person fred = new Person(101, "Fred", "Feuerstein", "ff@email.com");

    try {
      // Get an ObjectTransformer.
      // The ObjectTransformer interface offers all needed methods.
      ObjectTransformer trans =
        ObjectTransformerFactory.getInstance().getImplementation();

      // The transformer should create extra line feeds in the output.
      trans.setProperty(javax.xml.transform.OutputKeys.INDENT, "yes");

      // Set the amount of indenting if Xalan is used as XSL transformer.
      trans.setProperty("{http://xml.apache.org/xalan}indent-amount", "2");

      // Use western european encoding in the XML output.
      //trans.setProperty(javax.xml.transform.OutputKeys.ENCODING, "ISO-8859-1");

      // Serialize the Person object into a file.
      FileOutputStream out = new FileOutputStream("fred.xml");
      trans.serialize(fred, new StreamResult(out));
      out.close();

      // Create an XML schema file that describes the Person class.
      out = new FileOutputStream("Person.xsd");
      trans.writeXMLSchema(Person.class, new StreamResult(out));
      out.close();

      // Read a Person's XML file and create a new Person object
      // from its content.
      FileInputStream in = new FileInputStream("fred.xml");
      Person freddy = (Person) trans.deserialize(new StreamSource(in));
      in.close();

      // Check the result.
      if (fred.equals(freddy)) {
        System.out.println("OK");
      }
      else {
        System.out.println("ERROR");
      }

      // Demonstrate the setting of properties for the serializer

      trans.setProperty(com.skaringa.javaxml.PropertyKeys.OMIT_XSI_NIL, "true");
      trans.setProperty(
        com.skaringa.javaxml.PropertyKeys.OMIT_XSI_TYPE,
        "true");
      trans.setProperty(com.skaringa.javaxml.PropertyKeys.OMIT_ID, "true");
      // Serialize the Person object into a file.
      // Omit all xsi:type, xsi:nil and id attributes.
      // This will shorten the output and make it more "human readable",
      // but this may not work correctly for all kinds of objects.
      out = new FileOutputStream("fred_short.xml");
      trans.serialize(fred, new StreamResult(out));
      out.close();
      // ... and deserialize it again
      // Note that the transformer used for deserialization also needs
      // the property OMIT_XSI_TYPE
      in = new FileInputStream("fred_short.xml");
      Person freddyboy = (Person) trans.deserialize(new StreamSource(in));
      in.close();
      // Check the result.
      if (fred.equals(freddyboy)) {
        System.out.println("OK again");
      }
      else {
        System.out.println("ERROR 2");
      }

      // Demonstrate the Java object transformation

      // Create an XML schema file that describes the OrgMember class.
      out = new FileOutputStream("OrgMember.xsd");
      trans.writeXMLSchema(OrgMember.class, new StreamResult(out));
      out.close();

      // set the transformation stylesheet
      trans.setPostprocessorInstruction(
        new StreamSource(
          ClassLoader.getSystemResourceAsStream(
            "com/skaringa/javaxml/example/Person2OrgMember.xsl")));

      // transform using the previously set (and pre-parsed) stylesheet
      OrgMember fredTheMember = (OrgMember) trans.transform(fred);

      System.out.println(fredTheMember);
    }
    // Handle the weird things...
    catch (NoImplementationException e) {
View Full Code Here

          .DocumentBuilderFactory
          .newInstance()
          .newDocumentBuilder();
      org.w3c.dom.Document doc = docBuilder.newDocument();

      ObjectTransformer trans =
        ObjectTransformerFactory.getInstance().getImplementation();
      trans.serialize(people, new javax.xml.transform.dom.DOMResult(doc));

      // Use XPath to find the collection element
      // of the person whose last name is ...
      String xPathQuery = "//cel[lastName='Feuerstein']";
      org.w3c.dom.Node ffNode = null;

      // if Java 1.5 >>
//      javax.xml.xpath.XPath xpath =
//        javax.xml.xpath.XPathFactory.newInstance().newXPath();
//      ffNode =
//        (org.w3c.dom.Node) xpath.evaluate(
//          xPathQuery,
//          doc,
//          javax.xml.xpath.XPathConstants.NODE);
      // << endif Java 1.5

      // if Java 1.4 >>
//      ffNode = org.apache.xpath.XPathAPI.selectSingleNode(doc, xPathQuery);
      // << endif Java 1.4

      // deserialize the node into a person
      if (ffNode != null) {
        Person p =
          (Person) trans.deserialize(
            new javax.xml.transform.dom.DOMSource(ffNode));
        System.out.println(p);
      }
    }
    catch (Exception e) {
View Full Code Here

   */
  public static void main(String[] args) {

    try {
      // Get an ObjectTransformer.
      ObjectTransformer trans =
        ObjectTransformerFactory.getInstance().getImplementation();
      // The transformer should create extra line feeds in the output.
      trans.setProperty(javax.xml.transform.OutputKeys.INDENT, "yes");

      // set pre- and postprocessing instruction
      trans.setPreprocessorInstruction(
        new StreamSource(ClassLoader.getSystemResourceAsStream(XSL_PRE_RES)));
      trans.setPostprocessorInstruction(
        new StreamSource(ClassLoader.getSystemResourceAsStream(XSL_POST_RES)));

      // deserialize customized xml into a collection of strings
      // (using preprocessing instructions)
      Collection coll =
        (Collection) trans.deserialize(
          new StreamSource(ClassLoader.getSystemResourceAsStream(XML_IN_RES)));

      // work with the collection...
      Collection newColl = new ArrayList();
      Iterator it = coll.iterator();
      while (it.hasNext()) {
        newColl.add(firstUp((String) it.next()));
      }

      // serialize the collection into a customized xml string
      // (using postprocessing instructions)
      System.out.println(trans.serializeToString(newColl));
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
View Full Code Here

TOP

Related Classes of com.skaringa.javaxml.ObjectTransformer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.