SHARE
TWEET

aeeeeeeeee

a guest Jan 20th, 2020 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package br.com.mil.sc1.interc2.hermes;
  2.  
  3. import java.io.IOException;
  4. import java.io.StringReader;
  5. import java.io.StringWriter;
  6. import java.text.DateFormat;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.Iterator;
  10. import java.util.Map.Entry;
  11. import java.util.TimeZone;
  12.  
  13. import javax.xml.bind.JAXBContext;
  14. import javax.xml.bind.JAXBException;
  15. import javax.xml.bind.Marshaller;
  16. import javax.xml.bind.Unmarshaller;
  17.  
  18. import org.mip_interop.schemas.adem._2014._04._11.jc3actiontask.Plan;
  19.  
  20. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  21. import com.fasterxml.jackson.core.JsonParseException;
  22. import com.fasterxml.jackson.core.JsonProcessingException;
  23. import com.fasterxml.jackson.databind.DeserializationFeature;
  24. import com.fasterxml.jackson.databind.JsonMappingException;
  25. import com.fasterxml.jackson.databind.JsonNode;
  26. import com.fasterxml.jackson.databind.ObjectMapper;
  27. import com.fasterxml.jackson.databind.ObjectWriter;
  28. import com.fasterxml.jackson.databind.SerializationFeature;
  29.  
  30. public class Teste {
  31.  
  32.     /*
  33.      * <dependency> <groupId>com.fasterxml.jackson.core</groupId>
  34.      * <artifactId>jackson-databind</artifactId> <version>2.5.3</version>
  35.      * </dependency>
  36.      */
  37.  
  38.     public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
  39.  
  40.         String xml = "<ns6:Plan xmlns:ns6=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3ActionTask.xsd\" xmlns=\"urn:br:mil:md:sc1:interc2:types\" xmlns:ns2=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3Types.xsd\" xmlns:ns3=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3Common.xsd\" xmlns:ns4=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3ActionResource.xsd\" xmlns:ns5=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3ActionObjective.xsd\" xmlns:ns7=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3Location.xsd\" xmlns:ns8=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3Materiel.xsd\" xmlns:ns9=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3Facilities.xsd\" xmlns:ns10=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3ControlFeatures.xsd\" xmlns:ns11=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3Unit.xsd\" xmlns:ns12=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3Organisations.xsd\" xmlns:ns13=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3File.xsd\" xmlns:ns14=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3ActionFunctionalAssoc.xsd\" xmlns:ns15=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3ObjectItemAssociations.xsd\" xmlns:ns16=\"http://mip-interop.org/schemas/ADEM/2014/04/11/Jc3OrgActionAssoc.xsd\" uri=\"urn:uuid:be5143da-3ded-4e3c-bf49-573e4b3e2fb4\" Source=\"EB\">\n"
  41.                 + "    <ns3:Metadata>\n"
  42.                 + "        <ns3:ReportingDatetime>2019-05-23T22:28:47.352Z</ns3:ReportingDatetime>\n"
  43.                 + "        <ns3:EffectiveDatetimeStart>2019-05-19T09:00:00.000Z</ns3:EffectiveDatetimeStart>\n"
  44.                 + "        <ns3:EffectiveDatetimeEnd>2019-07-30T09:00:00.000Z</ns3:EffectiveDatetimeEnd>\n"
  45.                 + "    </ns3:Metadata>\n" + "    <ns6:NameTxt>Exercicio de Comunicacao</ns6:NameTxt>\n"
  46.                 + "    <ns6:PlndStartDttm>2019-05-19T09:00:00.000Z</ns6:PlndStartDttm>\n"
  47.                 + "    <ns6:PlndEndDttm>2019-07-30T09:00:00.000Z</ns6:PlndEndDttm>\n"
  48.                 + "    <ns6:Hostility>FR</ns6:Hostility>\n" + "    <ns7:VerticalPoint>\n"
  49.                 + "        <ns2:LatCoord>-15.779720</ns2:LatCoord>\n"
  50.                 + "        <ns2:LongCoord>-47.929720</ns2:LongCoord>\n"
  51.                 + "        <ns7:Altitude>1172.000000</ns7:Altitude>\n" + "    </ns7:VerticalPoint>\n" + "</ns6:Plan>\n"
  52.                 + "";
  53.  
  54.         // XML para java
  55.         JAXBContext jaxbContext;
  56.         Plan planXml = null;
  57.         try {
  58.             jaxbContext = JAXBContext.newInstance(Plan.class);
  59.  
  60.             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  61.  
  62.             planXml = (Plan) jaxbUnmarshaller.unmarshal(new StringReader(xml));
  63.         } catch (JAXBException e) {
  64.             e.printStackTrace();
  65.         }
  66.  
  67.         // java para json
  68.         ObjectWriter obj = new ObjectMapper().configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
  69.                 .setSerializationInclusion(Include.NON_NULL).writer().withRootName("Plan");
  70.  
  71.         String jsonStr = "";
  72.         try {
  73.  
  74.             jsonStr = obj.writeValueAsString(planXml);
  75.             System.out.println(jsonStr);
  76.  
  77.         } catch (IOException e) {
  78.             e.printStackTrace();
  79.         }
  80.  
  81.         System.out.println("json para java");
  82.         // json para java
  83.         ObjectMapper obj2 = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
  84.                 .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
  85.  
  86.         Plan planJson = obj2.readValue(jsonStr, Plan.class);
  87.  
  88.         ObjectMapper obj3 = new ObjectMapper();
  89.         JsonNode pegarRoot = obj3.readTree(jsonStr);
  90.  
  91.         getRootNodeValue(jsonStr);
  92.  
  93.         System.out.println(planJson.getUri().equals(planXml.getUri()));
  94.         System.out.println(planJson.getSource().equals(planXml.getSource()));
  95.         System.out.println(
  96.                 planJson.getMetadata().getReportingDatetime().equals(planXml.getMetadata().getReportingDatetime()));
  97.         System.out.println(planJson.getMetadata().getEffectiveDatetimeStart()
  98.                 .equals(planXml.getMetadata().getEffectiveDatetimeStart()));
  99.         System.out.println(planJson.getMetadata().getEffectiveDatetimeEnd());
  100.         System.out.println(planJson.getNameTxt());
  101.         System.out.println(planJson.getPlndStartDttm());
  102.         System.out.println(planJson.getPlndEndDttm());
  103.         System.out.println(planJson.getHostility());
  104.         System.out.println(planJson.getVerticalPoint().getLatCoord());
  105.         System.out.println(planJson.getVerticalPoint().getLongCoord());
  106.         System.out.println(planJson.getVerticalPoint().getAltitude());
  107.  
  108.         System.out.println("java para xml");
  109.  
  110.         // java para xml
  111.         String xmlNova = jaxbObjectToXML(planJson);
  112.  
  113.         System.out.println("xml para java");
  114.         // xml para java
  115.         JAXBContext jaxbContext2;
  116.         Plan plan3 = null;
  117.         try {
  118.             jaxbContext2 = JAXBContext.newInstance(Plan.class);
  119.  
  120.             Unmarshaller jaxbUnmarshaller = jaxbContext2.createUnmarshaller();
  121.  
  122.             plan3 = (Plan) jaxbUnmarshaller.unmarshal(new StringReader(xmlNova));
  123.         } catch (JAXBException e) {
  124.             e.printStackTrace();
  125.         }
  126.  
  127.         System.out.println(plan3.getUri());
  128.         System.out.println(plan3.getSource());
  129.         System.out.println(plan3.getMetadata().getReportingDatetime());
  130.         System.out.println(plan3.getMetadata().getEffectiveDatetimeStart());
  131.         System.out.println(plan3.getMetadata().getEffectiveDatetimeEnd());
  132.         System.out.println(plan3.getNameTxt());
  133.         System.out.println(plan3.getPlndStartDttm());
  134.         System.out.println(plan3.getPlndEndDttm());
  135.         System.out.println(plan3.getHostility());
  136.         System.out.println(plan3.getVerticalPoint().getLatCoord());
  137.         System.out.println(plan3.getVerticalPoint().getLongCoord());
  138.         System.out.println(plan3.getVerticalPoint().getAltitude());
  139.  
  140.     }
  141.  
  142.     private static String getRootNodeValue(String jsonString) throws JsonProcessingException, IOException {
  143.        
  144.         JsonNode rootNode = new ObjectMapper().readTree(new StringReader(jsonString));
  145.        
  146.         System.out.println("-------------");
  147.         String key = rootNode.fields().next().getKey();
  148.         System.out.println(key);
  149.         System.out.println("---------");
  150.        
  151.         return key;
  152.     }
  153.  
  154.     private static String jaxbObjectToXML(Plan plan) {
  155.         try {
  156.             JAXBContext jaxbContext = JAXBContext.newInstance(Plan.class);
  157.  
  158.             Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  159.  
  160.             jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  161.  
  162.             StringWriter sw = new StringWriter();
  163.  
  164.             jaxbMarshaller.marshal(plan, sw);
  165.  
  166.             String xmlContent = sw.toString();
  167.             System.out.println(xmlContent);
  168.             return xmlContent;
  169.  
  170.         } catch (JAXBException e) {
  171.             e.printStackTrace();
  172.         }
  173.  
  174.         return "deu M";
  175.     }
  176. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top