Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 15 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
15
Dung lượng
587,47 KB
Nội dung
Processing XML Using JavaLab03 Extensible Stylesheet Transformation Language Mục tiêu - Sửdụng transformer chuyển đổi file xml xsl (html) - Sửdụng XPath Expression để lấy liệu theo Expression từ file xml - Sửdụng JAXP APIs trình xử lý XPath Phần I Bài tập step by step Bài 3.1 In nội dung file emp.xsl chuyển đổi từ file Employees.xml: Employees.xml Emp.xsl IT Research Department @BKAP 2015 Trang / 15 Architecting Applications for the Web Step 1: Tạo project java netbean File New Project Java Java Application Project Name: Đặt tên Project ESTLExample Step 2: Tạo File Employees.xml Project New Other XML XML Document Employees.xml Allen Smith Alaska M IT Research Department @BKAP 2015 Trang / 15 Processing XML Using Java John Davis California M Bob Thomson Kansas M Step 3: Tạo File emp.xsl Project New Other XML XSL Stylesheet Emp.xsl IT Research Department @BKAP 2015 Trang / 15 Architecting Applications for the Web XML Data Admin Employee Step 4: Tạo lớp TransformDOM.java để chuyển đổi XML với XSL TransformDOM.java package estlexample; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.w3c.dom.Document; /** * * @author DELL */ public class TransformDOM { public static void TransformXMLToXsl(Document doc) { try { //Create Dom Source DOMSource source = new DOMSource(doc); //Create Transformer Factory TransformerFactory transformerFactory = TransformerFactory.newInstance(); //Print the type of XSLT processor implemented IT Research Department @BKAP 2015 Trang / 15 Processing XML Using Java System.out.println(transformerFactory.getClass()); //Create Transformer from Transformer Factory - Applying the stylesheet in the transformation Transformer tranformer = transformerFactory.newTransformer(new StreamSource("emp.xsl")); //An object to hold the results StreamResult result = new StreamResult(System.out); tranformer.transform(source, result); } catch (Exception ex) { ex.printStackTrace(); } } } Cấu trúc project sau hoàn thành Step 5: Triển khai hàm main ứng dụng ESTLExample.java package estlexample; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; /** * * @author DELL */ public class ESTLExample { /** * @param args the command line arguments IT Research Department @BKAP 2015 Trang / 15 Architecting Applications for the Web */ public static void main(String[] args) { try { File file = new File("Employees.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(file); //Transform the XML file into XSL TransformDOM tfd = new TransformDOM(); tfd.TransformXMLToXsl(doc); } catch (Exception ex) { ex.printStackTrace(); } } } Step 6: Build and Run ứng dụng Bài 3.2 Sửdụng biểu thức XPath in tất giá trị node firstname có file Employees.xml Step 1: Tạo lớp XpathExpressions.java để in tất giá trị node firstname XpathExpressions.java package estlexample; IT Research Department @BKAP 2015 Trang / 15 Processing XML Using Java import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; /** * * @author DELL */ public class XPathExpressions { public static void ExpressionFirstName(Document doc) { try { //Create XPath Factory XPathFactory xpathfac = XPathFactory.newInstance(); //Create XPath XPath xPath = xpathfac.newXPath(); //Create expression String expression = "/employees/employee/firstname"; //print expression System.out.println(expression); //get node list form Employees.xml and expression NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { //print node fristname System.out.println(nodeList.item(i) getFirstChild().getNodeValue()); } } catch (Exception e) { e.printStackTrace(); } } } Cấu trúc project sau hoàn thành IT Research Department @BKAP 2015 Trang / 15 Architecting Applications for the Web Step 2: Triển khai hàm main ứng dụng ESTLExample.java package estlexample; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; /** * * @author DELL */ public class ESTLExample { /** * @param args the command line arguments */ public static void main(String[] args) { try { File file = new File("Employees.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(file); //Transform the XML file into XSL // TransformDOM tfd = new TransformDOM(); // tfd.TransformXMLToXsl(doc); XPathExpressions xpath = new XPathExpressions(); xpath.ExpressionFirstName(doc); } catch (Exception ex) { ex.printStackTrace(); IT Research Department @BKAP 2015 Trang / 15 Processing XML Using Java } } } Step 3: Build and Run ứng dụng Bài 3.3 Sửdụng XPath Expression lấy liệu từ file Employees.xml In tất giá trị node với điều kiện giá trị attribute element employee có id =”112” In giá trị node element employee có giá trị node location = “Kansas” Step 1: Tạo method ExpressionEmployeeAttribute – in tất node có giá trị attribute element employee có id = “112” ExpressionNodeValue – in giá trị node element employee có giá trị node location = “Kansas” XpathExpression.java XpathExpresion.java … public static void ExpressionEmployeeAttribute(Document doc) { try { //Create XPath Factory XPathFactory xpathfac = XPathFactory.newInstance(); //Create XPath XPath xPath = xpathfac.newXPath(); IT Research Department @BKAP 2015 Trang / 15 Architecting Applications for the Web //Create expression String expression = "/employees/employee[@id='112']"; //print expression System.out.println(expression); //Get node employee from Employees.xml and expression Node node = (Node) xPath.compile(expression).evaluate(doc, XPathConstants.NODE); if (null != node) { NodeList nodeList = node.getChildNodes(); for (int i = 0; null != nodeList && i < nodeList.getLength(); i++) { //get all child node of node employee Node nod = nodeList.item(i); //print node if (nod.getNodeType() == Node.ELEMENT_NODE) { System.out.println(nodeList.item(i).getNodeName() + " : " + nod.getFirstChild().getNodeValue()); } } } } catch (Exception ex) { ex.printStackTrace(); } } public static void ExpressionNodeValue(Document doc) { try { //Create XPath Factory XPathFactory xpathfac = XPathFactory.newInstance(); //Create XPath XPath xPath = xpathfac.newXPath(); //Create expression String expression = "/employees/employee[location='Kansas']"; //print expression System.out.println(expression); //Get node employee from Employees.xml and expression Node node = (Node) xPath.compile(expression).evaluate(doc, XPathConstants.NODE); if (null != node) { //get all child node of node employee NodeList nodeList = node.getChildNodes(); for (int i = 0; null != nodeList && i < nodeList.getLength(); i++) { IT Research Department @BKAP 2015 Trang 10 / 15 Processing XML Using Java //print node Node nod = nodeList.item(i); if (nod.getNodeType() == Node.ELEMENT_NODE) { System.out.println(nodeList.item(i).getNodeName() + " : " + nod.getFirstChild().getNodeValue()); } } } } catch (Exception ex) { ex.printStackTrace(); } } Step 2: Triển khai hàm main ứng dụng package estlexample; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; /** * * @author DELL */ public class ESTLExample { /** * @param args the command line arguments */ public static void main(String[] args) { try { File file = new File("Employees.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(file); //Transform the XML file into XSL // TransformDOM tfd = new TransformDOM(); // tfd.TransformXMLToXsl(doc); IT Research Department @BKAP 2015 Trang 11 / 15 Architecting Applications for the Web XPathExpressions xpath = new XPathExpressions(); xpath.ExpressionFirstName(doc); xpath.ExpressionEmployeeAttribute(doc); xpath.ExpressionNodeValue(doc); } catch (Exception ex) { ex.printStackTrace(); } // } } Step 3: Build and Run ứng dụng Bài 3.4 Chỉnh employee có attribute giá trị id = “112” có node location có giá trị “Kansas” IT Research Department @BKAP 2015 Trang 12 / 15 Processing XML Using Java In hình giá trị node firstname node có giá trị node location Kansas In hình tổng số node có giá trị node location Kansas Nếu tổng số node có giá trị node location Kansas nhiều in hình “There are more than one employee located at Kansas” Step 1: Tạo method XPathUsingJAXPAPI lớp XpathExpression để in hình giá trị node firstname, tổng số node, kiểm tra tổng số node có lớn khơng node có giá trị node location Kansas XpathExpression.java … public static void XPathUsingJAXPAPI(Document doc) { try { //Create an XPathFactory XPathFactory xFactory = XPathFactory.newInstance(); //Create an XPath object XPath xpath = xFactory.newXPath(); //Compile the XPath expression XPathExpression expr = xpath.compile("/employees/employee[location='Kansas']/firstname/text()"); //Run the query and get a nodeset Object result = expr.evaluate(doc, XPathConstants.NODESET); //Cast the result to a DOM NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); } /* XPath expression to get the number of peoplelocated at Kansas */ expr = xpath.compile("count(/employees/employee[location='Kansas'])"); /* Run the query and get the number of nodes*/ Double number = (Double) expr.evaluate(doc, XPathConstants.NUMBER); System.out.println("Number of objects " + number); /* Check more than one employeelocated at Kansas*/ expr = xpath.compile("count(/employees/employee[location='Kansas']) >1"); /* Run the query and get the number of nodes*/ IT Research Department @BKAP 2015 Trang 13 / 15 Architecting Applications for the Web Boolean check = (Boolean) expr.evaluate(doc, XPathConstants.BOOLEAN); if (check == true) { System.out.println("There are more than one employee located at Kansas"); } } catch (Exception ex) { ex.printStackTrace(); } } Step 2: Triển khai hàm main ứng dụng package estlexample; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; /** * * @author DELL */ public class ESTLExample { /** * @param args the command line arguments */ public static void main(String[] args) { try { File file = new File("Employees.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(file); //Transform the XML file into XSL // TransformDOM tfd = new TransformDOM(); // tfd.TransformXMLToXsl(doc); XPathExpressions xpath = new XPathExpressions(); // xpath.ExpressionFirstName(doc); // xpath.ExpressionEmployeeAttribute(doc); // xpath.ExpressionNodeValue(doc); IT Research Department @BKAP 2015 Trang 14 / 15 Processing XML Using Java xpath.XPathUsingJAXPAPI(doc); } catch (Exception ex) { ex.printStackTrace(); } } } Step 3: Build and Run ứng dụng Phần II Bài tập tự làm IT Research Department @BKAP 2015 Trang 15 / 15 ... TransformDOM .java để chuyển đổi XML với XSL TransformDOM .java package estlexample; import javax .xml. transform.Transformer; import javax .xml. transform.TransformerFactory; import javax .xml. transform.dom.DOMSource;... Department @BKAP 2015 Trang / 15 Processing XML Using Java } } } Step 3: Build and Run ứng dụng Bài 3. 3 Sử dụng XPath Expression lấy liệu từ file Employees .xml In tất giá trị node với điều kiện... Processing XML Using Java import javax .xml. xpath.XPath; import javax .xml. xpath.XPathConstants; import javax .xml. xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; /** * * @author