1

I have a java class whose source code I have to send to a client. I want to send the source as a string / json / embedded into an xml. What would be the right way to stringify the java source code to create a valid xml / json ? Escaping quotes alone doesn't work.

Aneesh
  • 1,703
  • 3
  • 24
  • 34
  • 3
    have you already tried using CDATA sections inside the xml? – mschenk74 Jun 25 '14 at 07:27
  • Can I ask why you want to "stringify" the code? – dARKpRINCE Jun 25 '14 at 07:33
  • 1
    @mschenk74 Thanks, embedding in CDATA works and helps my needs. – Aneesh Jun 25 '14 at 07:37
  • Unless the java class contains the CDATA terminating characters. Just use an XML/JSON library for god's sake. – Christoffer Hammarström Jun 25 '14 at 08:27
  • 1
    Is it relevant that the data is Java source, or is this just a question about XML/JSON? – Chris Martin Jun 25 '14 at 08:32
  • try Jackson or gson to convert Java objects to json format, for xml you can use jaxb – AurA Jun 25 '14 at 08:36
  • @ChristofferHammarström I just mentioned CDATA as the next step after "Escaping quotes". I never meant it to be the perfect solution. I agree with you that this should be the job of a XML/JSON library. But I also didn't get the rationale for sending java source code via xml yet. I wonder if there is no better solution for the actual problem than sending the source code. – mschenk74 Jun 25 '14 at 09:16
  • @Aneesh See [What's so bad about building XML with string concatenation?](http://stackoverflow.com/questions/3034611/whats-so-bad-about-building-xml-with-string-concatenation) – Christoffer Hammarström Jun 25 '14 at 10:27

0 Answers0