Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sort;
- import org.w3c.dom.Document;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.w3c.dom.ls.DOMImplementationLS;
- import org.w3c.dom.ls.LSOutput;
- import org.w3c.dom.ls.LSSerializer;
- import org.xml.sax.SAXException;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.xpath.*;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- public class SplitXML {
- public static void printXmlDocument(Document document) throws IOException {
- DOMImplementationLS domImplementationLS =
- (DOMImplementationLS) document.getImplementation();
- LSSerializer lsSerializer =
- domImplementationLS.createLSSerializer();
- BufferedWriter bw=new BufferedWriter(new FileWriter(File.createTempFile("abc",".xml",new File("/tmp/abc"))));
- LSOutput lsOutput = domImplementationLS.createLSOutput();
- lsOutput.setCharacterStream(bw);
- lsSerializer.write(document, lsOutput);
- }
- public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
- DocumentBuilderFactory f=DocumentBuilderFactory.newInstance();
- f.setNamespaceAware(true);
- DocumentBuilder builder = f.newDocumentBuilder();
- Document doc = builder.parse("/home/akm/test.xml");
- XPathFactory xf=XPathFactory.newInstance();
- XPath xpath=xf.newXPath();
- XPathExpression exp=xpath.compile("//agreement[current_status='1']");
- NodeList res= (NodeList) exp.evaluate(doc, XPathConstants.NODESET);
- for(int i=0;i<res.getLength();i++){
- doc=f.newDocumentBuilder().newDocument();
- Node root = doc.importNode(res.item(i), true);
- doc.appendChild(root);
- printXmlDocument(doc);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement