-2

I have the following response in form of string and want to remove headers Content-Type and Content-Length from that string so that the resulting string has only xml data starting from < ?xml version ... upto < /Product>. How to do this in java.

Content-Type: application/xml
Content-Length: 107

<?xml version = "1.0" encoding = "UTF-8" ?>
<Product>
<Id>11001</Id>
<Time>2012-06-09 14:18:22</Time>
</Product>

1 Answers1

0

Since every xml document starts with the xml declaration <?xml version..., you may want to use .indexOf() and .substring() to get a string with only the xml document.

Assuming your String is called xmlString, it would probabily look like this:

xmlString = xmlString.substring(xmlString.indexOf("<?xml version"));

Hope this helps!

Pietro
  • 16
  • 2
  • “every xml document starts with the xml declaration…” No, they don’t. [The spec even says it’s optional.](https://www.w3.org/TR/REC-xml/#NT-prolog) – VGR Jun 09 '16 at 19:58
  • The version is mandatory, while the declaration isn't. I should have been more precise. src: http://stackoverflow.com/questions/7007427/does-a-valid-xml-file-require-an-xml-declaration – Pietro Jun 11 '16 at 09:58
  • Read those answers. It’s optional in XML 1.0, which is what most of the world is currently using. – VGR Jun 11 '16 at 12:41
  • Sorry, my bad. I must have missed it. I'll pay more attention next time :) – Pietro Jun 15 '16 at 14:54