0

I am sending the XML String with the time-stamp format "YYYY-MM-DDTH:M:S" but ideally the date time data type of the XSD expects the format to be "YYYY-MM-DDTHH:MM:SS" hence the processing is failing with an error message "Time-stamp is missing or invalid in XML file"

Due to this incorrect time-stamp format the timestmap is being set as null during unmarshaling the xml to the Product Object. So, the XML needs to be pre-processed with the correct timestmap when ever a wrong format is encountered. Could you suggest on the best approach to customize/update the timestamp value with the expected format and send the updated xml back? Example If this time-stamp encountered is "2016-10-13T2:18:41" it has to be changed to "2016-10-13T02:18:41" this applies to hours, minutes and seconds . Here is the piece of code for your reference and the xml string value to be passed(for example:)

 public Product parseXML(String xml) throws SASException {
    //String xml = "<product><timestamp>2015-10-30T1:41:22.109-04:00</timestamp></product>";

    Product product = null;
    try {
        XMLReader xmlReader = null;
        SAXParserFactory factory = SAXParserFactory.newInstance();

        factory.setNamespaceAware(true);
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        factory.setFeature(ProductConstants.FEATURE_EXTERNAL_GENERAL_ENTITY, false);
        factory.setFeature(ProductConstants.FEATURE_DISALLOW_DOCTYPE_DECLARE, false);

        xmlReader = factory.newSAXParser().getXMLReader();
        InputSource inSrc = new InputSource(new StringReader(xml));

        SAXSource saxSource = new SAXSource(xmlReader, inSrc);
        JAXBContext context = JAXBContext.newInstance(Product.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        product = (Product) unmarshaller.unmarshal(saxSource);

    } catch (ParserConfigurationException | SAXException | JAXBException exception) {
        LOG.error(ProductConstants.ERROR_CODE_XML_UNMARSHALLING_FAILED, exception);
        throw new SASException(ProductConstants.ERROR_CODE_XML_UNMARSHALLING_FAILED, exception);
    }
    LOG.info("Completed product parsing xml: product" + product);
    return product;
}
  • No, that post was about omitting the milliseconds from the xml string. however, in my case, the timestamp hour value comes with single digit(1-9) which needs to be prefixed with 0 so that unmarshalling will be success. If this time-stamp encountered is "2016-10-13T2:18:41" it has to be changed to "2016-10-13T02:18:41" this applies to hours, minutes and seconds . – user2248913 Oct 21 '16 at 13:56
  • 1
    Should be nearly the same process just a different format or maybe a customer string handler. In either case, that answer will give you access to the raw string for which you can do whatever processing you desire. – Andrew White Oct 21 '16 at 14:10
  • My Product class object generated from Product.xsd thru JAXB. So i am trying to fetch the timestamp value from the passed xml and trying to format its value and ensure the single digit after T prefiexed with 0. – user2248913 Oct 21 '16 at 14:56

0 Answers0