1. Trang chủ
  2. » Công Nghệ Thông Tin

Thực hành sử dụng XML trong Java Lab 6

10 179 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 584,61 KB

Nội dung

YEES SQL Database ghi file employees.xml từ file template-based EmployeeInfo.xml  Input  EmployeeInfo.xml  Table EMPLOYEES IT Research Department @BKAP 2015 Page / 10 Processing XML Using Java  Output Step 1: Tạo file EmployeeInfo.xml List of employees SELECT id,firstname,lastname,location FROM employees $firstName $lastName $location End of employeelist Step 2: Lấy bảng EMPLOYEE 6.1 làm Step 3: Tạo JavaMain project 6.1(XMLDatabase)  TemplateBase.java IT Research Department @BKAP 2015 Page / 10 Architecting Applications for the Web package xmldatabase; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * * @author DELL */ public class TemplateBase { /** * @param args the command line arguments */ public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException, SQLException, TransformerConfigurationException, TransformerException { //parse file EmployeeInfo and get value query File file = new File("EmployeeInfo.xml"); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document doc = documentBuilder.parse(file); doc.getDocumentElement().normalize(); NodeList nodeList = doc.getElementsByTagName("EmployeeInfo"); Node node = nodeList.item(0); Element eElement = (Element) node; String query = eElement.getElementsByTagName("SelectStmnt").item(0).getTextContent(); //connect database and get data from database to resultset String URL = "jdbc:sqlserver://localhost:1433;databaseName=Struts2"; String DB_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String user_db = "sa"; String pass_db = "Gacon1984"; Class.forName(DB_DRIVER); Connection conn = DriverManager.getConnection(URL,user_db,pass_db); Statement statement = conn.createStatement(); ResultSet rs = statement.executeQuery(query); //write from resultset to file xml IT Research Department @BKAP 2015 Page / 10 Processing XML Using Java Document docxml = documentBuilder.newDocument(); //create root element EmployeeInfo Element rootElement = docxml.createElement("EmployeeInfo"); docxml.appendChild(rootElement); //create element Introduction String itdText= eElement.getElementsByTagName("Introduction").item(0).getTextContent(); Element introduction = docxml.createElement("Introduction"); introduction.appendChild(docxml.createTextNode(itdText)); rootElement.appendChild(introduction); while (rs.next()) { //create element employee Element employee = docxml.createElement("employee"); employee.setAttribute("id", rs.getString("id")); //create element firstName,lastName,location of element employee Element firstname = docxml.createElement("firstName"); firstname.appendChild(docxml.createTextNode(rs.getString("firstname"))); Element lastname = docxml.createElement("lastName"); lastname.appendChild(docxml.createTextNode(rs.getString("lastname"))); Element location = docxml.createElement("location"); location.appendChild(docxml.createTextNode(rs.getString("location"))); employee.appendChild(firstname); employee.appendChild(lastname); employee.appendChild(location); //add employee to root element rootElement.appendChild(employee); } //create element Conclusion String cText= eElement.getElementsByTagName("Conclusion").item(0).getTextContent(); Element conclusion = docxml.createElement("Conclusion"); conclusion.appendChild(docxml.createTextNode(cText)); rootElement.appendChild(conclusion); //write file employee.xml TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tranformer = tfactory.newTransformer(); DOMSource source = new DOMSource(docxml); StreamResult result = new StreamResult(new File("employees.xml")); tranformer.transform(source, result); } } Step 4: Build and Run File TemplateBase.java IT Research Department @BKAP 2015 Page / 10 Architecting Applications for the Web Bài 6.3  Sử dụng Xquery in hình:  Tất giá trị element firstName file employees.xml sinh 6.2  Tất giá trị element firstName có location Kansas file employee.xml Step 1: Tạo java main class project XMLDatabase  XMLBased.java package xmldatabase; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; 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.NodeList; import org.xml.sax.SAXException; /** * * @author DELL IT Research Department @BKAP 2015 Page / 10 Processing XML Using Java */ public class XMLBased { /** * @param args the command line arguments */ public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { //parse file employees.xml with DOM DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("employees.xml")); //create Xpath XPathFactory xFactory = XPathFactory.newInstance(); XPath xPath = xFactory.newXPath(); //Xquery select all firstName String expression1 = "/EmployeeInfo/employee/firstName"; System.out.println(expression1); //get NodeList with expression1 NodeList nodelist1 = (NodeList) xPath.compile(expression1).evaluate(doc,XPathConstants.NODESET); //print all firstName with expression1 for (int i = 0; i < nodelist1.getLength(); i++) { System.out.println(nodelist1.item(i).getFirstChild().getNodeValue()); } //Xquery select firstName with location = Kansas String expression2 = "/EmployeeInfo/employee[location='Kansas']/firstName"; System.out.println(expression2); //get NodeList with expression2 NodeList nodelist2 = (NodeList) xPath.compile(expression2).evaluate(doc,XPathConstants.NODESET); //print all firstName with expression2 for (int j = 0; j < nodelist2.getLength(); j++) { System.out.println(nodelist2.item(j).getFirstChild().getNodeValue()); } } } Step 2: Build and Run File Phần II: Bài tập tự làm Phân tích file HangHoa.xml lấy nội dung file đưa vào table PRODUCT database IT Research Department @BKAP 2015 Page / 10 Architecting Applications for the Web IT Research Department @BKAP 2015 Page 10 / 10 ... main class project XMLDatabase  XMLBased .java package xmldatabase; import java. io.File; import java. io.IOException; import javax .xml. parsers.DocumentBuilder; import javax .xml. parsers.DocumentBuilderFactory;... import javax .xml. parsers.ParserConfigurationException; import javax .xml. xpath.XPath; import javax .xml. xpath.XPathConstants; import javax .xml. xpath.XPathExpressionException; import javax .xml. xpath.XPathFactory;... javax .xml. transform.TransformerConfigurationException; import javax .xml. transform.TransformerException; import javax .xml. transform.TransformerFactory; import javax .xml. transform.dom.DOMSource; import javax .xml. transform.stream.StreamResult;

Ngày đăng: 07/05/2018, 16:20

TỪ KHÓA LIÊN QUAN

w