Total members 11890 |It is currently Sun Jun 16, 2024 7:18 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





evaluate XPath Expression from XML file.
java code
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;



public class XPathUtility {


private XPathUtility() {

}


public static String evaluateNode(String expression, InputStream xmlFile) {
Object[] params = { expression, xmlFile };


Node result = null;

DocumentBuilder parser = null;
try {
parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException parserConfigurationException) {
// Do Something
}
Document doc = null;
try {
doc = parser.parse(xmlFile);
} catch (SAXException saxException) {
// Do Something
} catch (IOException ioException) {
// Do Something
}

XPath xpath = XPathFactory.newInstance().newXPath();

try {

result = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);

} catch (XPathExpressionException xPathExpressionException) {
// Do Something
}

Document parsedDoc = null;
OutputStream os = null;

try {
parsedDoc = parser.newDocument();
Node importedNode = parsedDoc.importNode(result, true);
os = new ByteArrayOutputStream();

// Prepare the DOM document for writing
Source source = new DOMSource(importedNode);

// Prepare the output String
Result outpuResult = new StreamResult(os);

// Write the DOM document to the outputStream
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
xformer.transform(source, outpuResult);

} catch (TransformerFactoryConfigurationError e) {
// Do Something
} catch (TransformerException e) {
// Do Something
}


return os.toString();
}

}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : evaluate XPath Expression from XML file
 Parse XML using DOM file and run XPath query     -  
 How to Evaluate a Research Proposal     -  
 java regular expression     -  
 email adress validation and regular expression     -  
 Encrypt/Decrypt a file from source file to target file.     -  
 Copy file to file in java code- implementation     -  
 file descriptor vs file pointer     -  
 getting file name of html input file tag using jsp     -  
 How to convert xml file to Pdf file using C     -  
 C++ File I/O     -  



Topic Tags

Java XML
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com