7.Lt Java Bai Tap_xml

12
Trang 78 7 XML PROCESSING Mc tiêu: Hiểu kiến thức về xử của ngôn ngữ đánh dấu mở rộng (eXtensible Markup Language). Hiểu rõ và áp dụng được phương pháp Parsing XML-document dùng DOM (Document Object Model). Hiểu rõ và áp dụng được phương pháp Parsing XML document dùng SAX (Simple API for XLM Parsing). Bài tập 1. Đọc tp tin XML trong ngôn nglp trình Java dùng DOM Parser. Ly node bng “name” và hin thgiá tr. To tp tin XML /Users/staff.xml <?xml version="1.0"?> <company> <staff id="1001"> <firstname>yong</firstname> <lastname>mook kim</lastname> <nickname>mkyong</nickname> <salary>100000</salary> </staff> <staff id="2001"> <firstname>low</firstname> <lastname>yin fong</lastname> <nickname>fong fong</nickname> <salary>200000</salary> </staff> </company> ReadXMLFile.java import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.io.File; public class ReadXMLFile { public static void main(String argv[]) { try { File fXmlFile = new File("/Users/staff.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

description

bai tap Xml

Transcript of 7.Lt Java Bai Tap_xml

  • Trang 78

    7 XML PROCESSING

    Mc tiu:

    Hiu r kin thc v x l ca ngn ng nh du m rng (eXtensible Markup Language).

    Hiu r v p dng c phng php Parsing XML-document dng DOM (Document Object

    Model).

    Hiu r v p dng c phng php Parsing XML document dng SAX (Simple API for XLM

    Parsing).

    Bi tp 1. c tp tin XML trong ngn ng lp trnh Java dng DOM Parser. Ly node bng

    name v hin th gi tr.

    To tp tin XML

    /Users/staff.xml

    yong

    mook kim

    mkyong

    100000

    low

    yin fong

    fong fong

    200000

    ReadXMLFile.java

    import javax.xml.parsers.DocumentBuilderFactory;

    import javax.xml.parsers.DocumentBuilder;

    import org.w3c.dom.Document;

    import org.w3c.dom.NodeList;

    import org.w3c.dom.Node;

    import org.w3c.dom.Element;

    import java.io.File;

    public class ReadXMLFile {

    public static void main(String argv[]) {

    try {

    File fXmlFile = new File("/Users/staff.xml");

    DocumentBuilderFactory dbFactory =

    DocumentBuilderFactory.newInstance();

  • Trang 79

    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

    Document doc = dBuilder.parse(fXmlFile);

    doc.getDocumentElement().normalize();

    System.out.println("Root element :" +

    doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("staff");

    System.out.println("----------------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {

    Node nNode = nList.item(temp);

    System.out.println("\nCurrent Element :" +

    nNode.getNodeName());

    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

    Element eElement = (Element) nNode;

    System.out.println("Staff id : " +

    eElement.getAttribute("id"));

    System.out.println("First Name : " +

    eElement.getElementsByTagName("firstname").item(0).getTextContent());

    System.out.println("Last Name : " +

    eElement.getElementsByTagName("lastname").item(0).getTextContent());

    System.out.println("Nick Name : " +

    eElement.getElementsByTagName("nickname").item(0).getTextContent());

    System.out.println("Salary : " +

    eElement.getElementsByTagName("salary").item(0).getTextContent());

    }

    }

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }

    Bi tp 2. c tp tin XML, hin th tng node (tn, gi tr v thuc tnh nu c)

    import java.io.File;

    import javax.xml.parsers.DocumentBuilder;

    import javax.xml.parsers.DocumentBuilderFactory;

    import org.w3c.dom.Document;

    import org.w3c.dom.NamedNodeMap;

    import org.w3c.dom.Node;

    import org.w3c.dom.NodeList;

    public class ReadXMLFile2 {

    public static void main(String[] args) {

  • Trang 80

    try {

    File file = new File("/Users/staff.xml");

    DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()

    .newDocumentBuilder();

    Document doc = dBuilder.parse(file);

    System.out.println("Root element :" +

    doc.getDocumentElement().getNodeName());

    if (doc.hasChildNodes()) {

    printNote(doc.getChildNodes());

    }

    } catch (Exception e) {

    System.out.println(e.getMessage());

    }

    }

    private static void printNote(NodeList nodeList) {

    for (int count = 0; count < nodeList.getLength(); count++) {

    Node tempNode = nodeList.item(count);

    if (tempNode.getNodeType() == Node.ELEMENT_NODE) {

    // ly tn node v gi tr node

    System.out.println("\nNode Name =" + tempNode.getNodeName() +

    " [OPEN]");

    System.out.println("Node Value =" +

    tempNode.getTextContent());

    if (tempNode.hasAttributes()) {

    // ly cc gi tr thuc tnh v tn thuc tnh

    NamedNodeMap nodeMap = tempNode.getAttributes();

    for (int i = 0; i < nodeMap.getLength(); i++) {

    Node node = nodeMap.item(i);

    System.out.println("attr name : " +

    node.getNodeName());

    System.out.println("attr value : " +

    node.getNodeValue());

    }

    }

    if (tempNode.hasChildNodes()) {

    // loop again if has child nodes

    printNote(tempNode.getChildNodes());

    }

    System.out.println("Node Name =" + tempNode.getNodeName() + "

    [CLOSE]");

    }

    }

  • Trang 81

    }

    }

    Bi tp 3. Dng DOM parser sa ni dung tp tin XML.

    1. Thm 1 element 2. Cp nht thuc tnh ( c) ca mt element. 3. Cp nht gi tr ( c) ca mt element. 4. Xa mt thnh phn

    file.xml Original XML file.

    yong

    mook kim

    mkyong

    100000

    DOM XML parser cp nht tp tin XML.

    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.transform.Transformer;

    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.NamedNodeMap;

    import org.w3c.dom.Node;

    import org.w3c.dom.NodeList;

    import org.xml.sax.SAXException;

    public class ModifyXMLFile {

    public static void main(String argv[]) {

    try {

    String filepath = "c:\\file.xml";// ng dn tp tin

  • Trang 82

    DocumentBuilderFactory docFactory =

    DocumentBuilderFactory.newInstance();

    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = docBuilder.parse(filepath);

    // Get the root element

    Node company = doc.getFirstChild();

    // Get the staff element , it may not working if tag has

    spaces, or

    // whatever weird characters in front...it's better to use

    // getElementsByTagName() to get it directly.

    // Node staff = company.getFirstChild();

    // Get the staff element by tag name directly

    Node staff = doc.getElementsByTagName("staff").item(0);

    // update staff attribute

    NamedNodeMap attr = staff.getAttributes();

    Node nodeAttr = attr.getNamedItem("id");

    nodeAttr.setTextContent("2");

    // append a new node to staff

    Element age = doc.createElement("age");

    age.appendChild(doc.createTextNode("28"));

    staff.appendChild(age);

    // loop the staff child node

    NodeList list = staff.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {

    Node node = list.item(i);

    // get the salary element, and update the value

    if ("salary".equals(node.getNodeName())) {

    node.setTextContent("2000000");

    }

    //remove firstname

    if ("firstname".equals(node.getNodeName())) {

    staff.removeChild(node);

    }

    }

    // ghi ni dung vo tp tin XML

    TransformerFactory transformerFactory =

    TransformerFactory.newInstance();

    Transformer transformer = transformerFactory.newTransformer();

    DOMSource source = new DOMSource(doc);

  • Trang 83

    StreamResult result = new StreamResult(new File(filepath));

    transformer.transform(source, result);

    System.out.println("Done");

    } catch (ParserConfigurationException pce) {

    pce.printStackTrace();

    } catch (TransformerException tfe) {

    tfe.printStackTrace();

    } catch (IOException ioe) {

    ioe.printStackTrace();

    } catch (SAXException sae) {

    sae.printStackTrace();

    }

    }

    }

    Bi tp 4. To mt tp tin XML mi (endcoding UTF-8).

    import java.io.File;

    import javax.xml.parsers.DocumentBuilder;

    import javax.xml.parsers.DocumentBuilderFactory;

    import javax.xml.parsers.ParserConfigurationException;

    import javax.xml.transform.Transformer;

    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.Attr;

    import org.w3c.dom.Document;

    import org.w3c.dom.Element;

    public class WriteXMLFile {

    public static void main(String argv[]) {

    try {

    DocumentBuilderFactory docFactory =

    DocumentBuilderFactory.newInstance();

    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    // root elements

    Document doc = docBuilder.newDocument();

    Element rootElement = doc.createElement("company");

    doc.appendChild(rootElement);

    // staff elements

  • Trang 84

    Element staff = doc.createElement("Staff");

    rootElement.appendChild(staff);

    // set attribute to staff element

    Attr attr = doc.createAttribute("id");

    attr.setValue("1");

    staff.setAttributeNode(attr);

    // shorten way

    // staff.setAttribute("id", "1");

    // firstname elements

    Element firstname = doc.createElement("firstname");

    firstname.appendChild(doc.createTextNode("yong"));

    staff.appendChild(firstname);

    // lastname elements

    Element lastname = doc.createElement("lastname");

    lastname.appendChild(doc.createTextNode("mook kim"));

    staff.appendChild(lastname);

    // nickname elements

    Element nickname = doc.createElement("nickname");

    nickname.appendChild(doc.createTextNode("mkyong"));

    staff.appendChild(nickname);

    // salary elements

    Element salary = doc.createElement("salary");

    salary.appendChild(doc.createTextNode("100000"));

    staff.appendChild(salary);

    // write the content into xml file

    TransformerFactory transformerFactory =

    TransformerFactory.newInstance();

    Transformer transformer = transformerFactory.newTransformer();

    DOMSource source = new DOMSource(doc);

    StreamResult result = new StreamResult(new

    File("C:\\file.xml"));

    // Output to console for testing

    // StreamResult result = new StreamResult(System.out);

    transformer.transform(source, result);

    System.out.println("File saved!");

    } catch (ParserConfigurationException pce) {

    pce.printStackTrace();

    } catch (TransformerException tfe) {

    tfe.printStackTrace();

  • Trang 85

    }

    }

    }

    Bi tp 5. m cc thnh phn ca tp tin XML thng qua DOM Parser (Document Object Model).

    Tp tin: file.xml

    yong

    mook kim

    mkyong

    2000000

    29

    low

    yin fong

    fong fong

    1000000

    Ali

    Baba

    Alibaba

    199000

    40

    Tp tin : CountXMLElement.java

    import java.io.IOException;

    import javax.xml.parsers.DocumentBuilder;

    import javax.xml.parsers.DocumentBuilderFactory;

    import javax.xml.parsers.ParserConfigurationException;

    import org.w3c.dom.Document;

    import org.w3c.dom.NodeList;

    import org.xml.sax.SAXException;

    public class CountXMLElement {

    public static void main(String argv[]) {

    try {

    String filepath = "c:\\file.xml";

    DocumentBuilderFactory docFactory =

    DocumentBuilderFactory.newInstance();

  • Trang 86

    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = docBuilder.parse(filepath);

    NodeList list = doc.getElementsByTagName("staff");

    System.out.println("Total of elements : " + list.getLength());

    } catch (ParserConfigurationException pce) {

    pce.printStackTrace();

    } catch (IOException ioe) {

    ioe.printStackTrace();

    } catch (SAXException sae) {

    sae.printStackTrace();

    }

    }

    }

    Bi tp 6. c tp tin XML dng SAX Parser.

    To tp tin XML n gin.

    yong

    mook kim

    mkyong

    100000

    low

    yin fong

    fong fong

    200000

    Dng SAX Parser phn tch tp tin XML.

    import javax.xml.parsers.SAXParser;

    import javax.xml.parsers.SAXParserFactory;

    import org.xml.sax.Attributes;

    import org.xml.sax.SAXException;

    import org.xml.sax.helpers.DefaultHandler;

    public class ReadXMLFile {

    public static void main(String argv[]) {

    try {

  • Trang 87

    SAXParserFactory factory = SAXParserFactory.newInstance();

    SAXParser saxParser = factory.newSAXParser();

    DefaultHandler handler = new DefaultHandler() {

    boolean bfname = false;

    boolean blname = false;

    boolean bnname = false;

    boolean bsalary = false;

    public void startElement(String uri, String localName,String qName,

    Attributes attributes) throws SAXException {

    System.out.println("Start Element :" + qName);

    if (qName.equalsIgnoreCase("FIRSTNAME")) {

    bfname = true;

    }

    if (qName.equalsIgnoreCase("LASTNAME")) {

    blname = true;

    }

    if (qName.equalsIgnoreCase("NICKNAME")) {

    bnname = true;

    }

    if (qName.equalsIgnoreCase("SALARY")) {

    bsalary = true;

    }

    }

    public void endElement(String uri, String localName,

    String qName) throws SAXException {

    System.out.println("End Element :" + qName);

    }

    public void characters(char ch[], int start, int length) throws

    SAXException {

    if (bfname) {

    System.out.println("First Name : " + new String(ch,

    start, length));

    bfname = false;

    }

    if (blname) {

  • Trang 88

    System.out.println("Last Name : " + new String(ch,

    start, length));

    blname = false;

    }

    if (bnname) {

    System.out.println("Nick Name : " + new String(ch,

    start, length));

    bnname = false;

    }

    if (bsalary) {

    System.out.println("Salary : " + new String(ch, start,

    length));

    bsalary = false;

    }

    }

    };

    saxParser.parse("c:\\file.xml", handler);

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }

    Bi tp 7. Chuyn thuc tnh thnh tp tin XML.

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.OutputStream;

    import java.util.Properties;

    public class PropertiesXMLExample

    {

    public static void main(String[] args) throws IOException

    {

    Properties props = new Properties();

    props.setProperty("email.support", "[email protected]");

    //where to store?

    OutputStream os = new FileOutputStream("c:/email-conf.xml");

    //store the properties detail into a pre-defined XML file

  • Trang 89

    props.storeToXML(os, "Support Email","UTF-8");

    System.out.println("Done");

    }

    }

    Kt qu tp tin XML c:/email-conf.xml.

    Support Email

    [email protected]

    Bi tp 8. Chuyn tp tin XML thnh tp tin thuc tnh

    c tp tin XML:

    Support Email

    [email protected]

    Dng phng thc loadFromXML() ly ni dung tp tin XML a vo i tng

    thuc tnh properties bng phng thc getProperty() .

    import java.io.FileInputStream;

    import java.io.IOException;

    import java.io.InputStream;

    import java.util.Properties;

    public class PropertiesXMLExample

    {

    public static void main(String[] args) throws IOException

    {

    Properties props = new Properties();

    InputStream is = new FileInputStream("c:/email-configuration.xml");

    //load the xml file into properties format

    props.loadFromXML(is);

    String email = props.getProperty("email.support");

    System.out.println(email);

    }

    }