[JAVA-XML]Hàm sửa đổi tệp XML bằng Java – (JDOM)

Thảo luận trong 'Java/Java Script' bắt đầu bởi IME, 25 Tháng tư 2011.

  1. Offline

    IME

    • Quy ẩn giang hồ

    Số bài viết:
    219
    Đã được thích:
    196
    Điểm thành tích:
    240
    Nội dung file xml
    Mã:
     
    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
     <company>   
     <staff id="1">      
      <firstname>yong</firstname>  
          <lastname>mook kim</lastname>     
       <nickname>mkyong</nickname>  
        <salary>100000</salary> 
     </staff></company>
    
    Mã:
     
     
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]java.io.File[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]java.io.IOException[/COLOR][COLOR=#339933];[/COLOR]
     
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]javax.xml.parsers.DocumentBuilder[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]javax.xml.parsers.DocumentBuilderFactory[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]javax.xml.parsers.ParserConfigurationException[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]javax.xml.transform.Transformer[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]javax.xml.transform.TransformerException[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]javax.xml.transform.TransformerFactory[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]javax.xml.transform.dom.DOMSource[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]javax.xml.transform.stream.StreamResult[/COLOR][COLOR=#339933];[/COLOR]
     
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]org.w3c.dom.Document[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]org.w3c.dom.Element[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]org.w3c.dom.NamedNodeMap[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]org.w3c.dom.Node[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]org.w3c.dom.NodeList[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000000][B]import[/B][/COLOR] [COLOR=#006699]org.xml.sax.SAXException[/COLOR][COLOR=#339933];[/COLOR]
     
    Void modified(String url)
    {
    [COLOR=#003399]String[/COLOR] filepath [COLOR=#339933]=[/COLOR] url[COLOR=#339933];[/COLOR]
    DocumentBuilderFactory docFactory [COLOR=#339933]=[/COLOR] DocumentBuilderFactory.[COLOR=#006633]newInstance[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
    DocumentBuilder docBuilder [COLOR=#339933]=[/COLOR] docFactory.[COLOR=#006633]newDocumentBuilder[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#003399]Document[/COLOR] doc [COLOR=#339933]=[/COLOR] docBuilder.[COLOR=#006633]parse[/COLOR][COLOR=#009900]([/COLOR]filepath[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
     
    [COLOR=#666666][I]// lấy phần tử gốc[/I][/COLOR]
      Node company [COLOR=#339933]=[/COLOR] doc.[COLOR=#006633]getFirstChild[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] 
    [COLOR=#666666][I]//Node staff = company.getFirstChild();[/I][/COLOR]
     
     [COLOR=#666666][I]//Phương thức getElementsByTagName() sẽ lấy phần tử bằng tên nut một cách chính xác.[/I][/COLOR]
             [COLOR=#666666][I]//Ở đây lấy phần tử staff đầu tiên bằng tên nốt[/I][/COLOR]
             Node staff [COLOR=#339933]=[/COLOR] doc.[COLOR=#006633]getElementsByTagName[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]"staff"[/COLOR][COLOR=#009900])[/COLOR].[COLOR=#006633]item[/COLOR][COLOR=#009900]([/COLOR][COLOR=#cc66cc]0[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
     
    [COLOR=#666666][I][COLOR=silver]//cập nhật thuộc tính cho staff[/COLOR][/I][/COLOR]
    NamedNodeMap attr [COLOR=#339933]=[/COLOR] staff.[COLOR=#006633]getAttributes[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
    Node nodeAttr [COLOR=#339933]=[/COLOR] attr.[COLOR=#006633]getNamedItem[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]"id"[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
    nodeAttr.[COLOR=#006633]setTextContent[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]"2"[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
     
    [COLOR=#666666][I][COLOR=silver]// [COLOR=#003399]thêm vào một nút mới cho staff(nut age)[/COLOR][/COLOR][/I][/COLOR]
    [COLOR=#003399]Element[/COLOR] age [COLOR=#339933]=[/COLOR] doc.[COLOR=#006633]createElement[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]"age"[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
    age.[COLOR=#006633]appendChild[/COLOR][COLOR=#009900]([/COLOR]doc.[COLOR=#006633]createTextNode[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]"28"[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
    staff.[COLOR=#006633]appendChild[/COLOR][COLOR=#009900]([/COLOR]age[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
     
    [COLOR=#666666][I]//vòng lặp duyệt các phần tử con của staff[/I][/COLOR]
    NodeList list [COLOR=#339933]=[/COLOR] staff.[COLOR=#006633]getChildNodes[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
     
    [COLOR=#000000][B]for[/B][/COLOR] [COLOR=#009900]([/COLOR][COLOR=#000066][B]int[/B][/COLOR] i [COLOR=#339933]=[/COLOR][COLOR=#cc66cc]0[/COLOR][COLOR=#339933];[/COLOR] i[COLOR=#339933]<[/COLOR]list.[COLOR=#006633]getLength[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]i[COLOR=#339933]++[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900]{[/COLOR]
    Node node [COLOR=#339933]=[/COLOR] list.[COLOR=#006633]item[/COLOR][COLOR=#009900]([/COLOR]i[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
     
    [COLOR=#666666][I]// lấy phần tử salary và cập nhật giá trị cho nó.[/I][/COLOR]
    [COLOR=#000000][B]if[/B][/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]"salary"[/COLOR].[COLOR=#006633]equals[/COLOR][COLOR=#009900]([/COLOR]node.[COLOR=#006633]getNodeName[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900]{[/COLOR]
    node.[COLOR=#006633]setTextContent[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]"2000000"[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#009900]}[/COLOR]
    [COLOR=#009900]}[/COLOR]
     
        [COLOR=silver] [COLOR=#666666][I]// ghi nội dung ra file xml[/I][/COLOR][/COLOR]
         TransformerFactory transformerFactory [COLOR=#339933]=[/COLOR] TransformerFactory.[COLOR=#006633]newInstance[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
         Transformer transformer [COLOR=#339933]=[/COLOR] transformerFactory.[COLOR=#006633]newTransformer[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
         DOMSource source [COLOR=#339933]=[/COLOR] [COLOR=#000000][B]new[/B][/COLOR] DOMSource[COLOR=#009900]([/COLOR]doc[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
         StreamResult result [COLOR=#339933]=[/COLOR]  [COLOR=#000000][B]new[/B][/COLOR] StreamResult[COLOR=#009900]([/COLOR][COLOR=#000000][B]new[/B][/COLOR] [COLOR=#003399]File[/COLOR][COLOR=#009900]([/COLOR]filepath[COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
         transformer.[COLOR=#006633]transform[/COLOR][COLOR=#009900]([/COLOR]source, result[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
     
     
    }
    

Chia sẻ trang này

Advertising: Linux system admin | nukeviet | nukeviet 4 | Upload ảnh miễn phí