Decoding binary data stored in XML by a third party tool Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Decoding binary data stored in XML by a third party tool without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m trying to parse XML files produced by a third party tool ( [HTC Sync Manager][hsm] ) , since said tool refuses to work anymore.

XML parsing is simple enough, but I’m stuck with trying to decode binary image data serialized into those XML files. As an example, I have a 886 character string which I know represents an image. The full string is

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/w AARCAA8AFADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGV mZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD9rP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zef8en2T/j6r6A/aF/4Ju6/wCAv2doPitf67olxqF1/wAhjSvtf2a5tev/AD917J/wRX0rRv2pLO+u/i5o1tctYWY+z/Y08kLz/smvpOTSPCXx38f3/wCz58ZfAmjeJNC8OSbNIm1SzD3cA9psg1zYDDKtjP3up62BynFUcHajWaP/2Q==

Base64 decoding it leads to garbage, so that’s not the answer.

How can I decode this kind of data?

Answer

As it happens, the problem was somewhere else. I am parsing the XML file using a SAX ContentHandler, e.g.

reader.setContentHandler(new DefaultHandler2() {

            private String currentLocalName;

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {

                currentLocalName = localName;
            }

            @Override
            public void characters(char[] ch, int start, int length) throws SAXException {
                  // read data here
         }

My problem was that the characters method is invoked multiple times, and the raw image data would be overwritten and only the last part would be kept.

The raw data is indeed Base64 encoded.

We are here to answer your question about Decoding binary data stored in XML by a third party tool - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji