* @param xmlPathQuery XML path query
* @param captureContent capture child content
* @return {@code Xml}
*/
public Xml read(String xmlPathQuery, boolean captureContent) {
XmlPath xmlPath = XmlPathParser.parse(xmlPathQuery);
Xml doc = new Xml();
boolean record = false;
int levelIndex = 0;
XmlNode node;
while((node = pullXmlNode()) != null) {
if(node instanceof XmlStartElement) {
XmlStartElement startElement = (XmlStartElement) node;
Element element = new Element(startElement.getLocalName());
element.addAttributes(startElement.getAttributes());
currentPath.addLast(element);
if(xmlPath.matches(currentPath)) {
if(!captureContent) {
// capture single element only
doc.addElement(element);
return doc;
}
// turn on document builder
record = true;
}
if(record) {
// build document
doc.addElement(element);
levelIndex += 1;
}
} else if(node instanceof XmlEndElement) {
if(xmlPath.isSimplePattern() && levelIndex == 0) {
// a simple pattern query can not read further up
nodeQueue.push(node);
return null;
}
if(currentPath.getLast() instanceof Content) {