1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package xml.xbean; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; public class XbeanFilterFeed implements main.IFilterFeed{ public OutputStream filter(InputStream is, String xpathExpr){ OutputStream os = System.out; try { // parse input stream into XML object XmlObject input = XmlObject.Factory.parse(is); //create output XML object XmlObject output = XmlObject.Factory.newInstance(); //generate a copy version of input output = input.copy(); //new XML cursor XmlCursor outputCursor = output.newCursor(); //declare namespace using XML Object Xpath syntax String expression = "declare namespace atom = 'http://www.w3.org/2005/Atom';" + "declare namespace row = 'http://www.ibm.com/xmlns/atom/content/datarow/1.0';"; //append the xpath expression after namespace declaration expression = expression + "atom:feed/atom:entry[not(" + xpathExpr + ")]"; // XML cursor performs xpath evaluation outputCursor.selectPath(expression); while(outputCursor.hasNextSelection()){ outputCursor.toNextSelection(); outputCursor.removeXml(); } //dispose XML cursor outputCursor.dispose(); //write XML object to output Stream output.save(os); } catch (XmlException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return os; } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |