code: public void isWelformed(String xml) {
          StringReader reader = new StringReader(xml);
          XMLStreamReader r = null;
          try {
              r = XML_INPUT_FACTORY.createXMLStreamReader(reader);
              while (r.hasNext()) {
                  r.next();
              }
          } catch (XMLStreamException e) {
              throw new RuntimeException(e);
          } finally {
              if (r != null) {
                  try {
                      r.close();
                  } catch (Exception e) {
                      // should not happen
                  }
              }
              reader.close();
          }
      }
docs: Checks whether supplied string contains a well formed XML document.
  @param xml xml document
  @throws RuntimeException when xml could not be parsed.