code: public static XElement parseXMLActiveFragment(XMLStreamReader in) throws XMLStreamException {
          XElement node = null;
          XElement root = null;
          final StringBuilder emptyBuilder = new StringBuilder();
          StringBuilder b = null;
          Deque<StringBuilder> stack = new LinkedList<>();
  
          int type = in.getEventType();
          
          for (;;) {
              switch(type) {
              case XMLStreamConstants.START_ELEMENT:
                  if (b != null) {
                      stack.push(b);
                      b = null;
                  } else {
                      stack.push(emptyBuilder);
                  }
                  XElement n = new XElement(in.getName().getLocalPart());
                  n.parent = node;
                  int attCount = in.getAttributeCount();
                  if (attCount > 0) {
                      for (int i = 0; i < attCount; i++) {
                          n.set(in.getAttributeLocalName(i), in.getAttributeValue(i));
                      }
                  }
                  if (node != null) {
                      node.add(n);
                  }
                  node = n;
                  if (root == null) {
                      root = n;
                  }
                  break;
              case XMLStreamConstants.CDATA:
              case XMLStreamConstants.CHARACTERS:
                  if (node != null && !in.isWhiteSpace()) {
                      if (b == null) {
                          b = new StringBuilder();
                      }
                      b.append(in.getText());
                  }
                  break;
              case XMLStreamConstants.END_ELEMENT:
                  if (node != null) {
                      if (b != null) {
                          node.content = b.toString();
                      }
                      node = node.parent;
                  }
                  b = stack.pop();
                  if (b == emptyBuilder) {
                      b = null;
                  }
                  if (stack.isEmpty()) {
                      return root;
                  }
                  break;
              default:
                  // ignore others.
              }
              
              if (in.hasNext()) {
                  type = in.next();
              } else {
                  break;
              }
          }
          return root;
      }
docs: Parses the stream as a fragment from the current element and returns an XElement.
  @param in the XML stream reader
  @return the parsed XElement instance
  @throws XMLStreamException in case there is a parsing error