簡體   English   中英

使用 PHP Dom 創建 xml 文件

[英]create xml file using Php Dom

我目前正在使用具有這種模式的 php Dom 創建一個 xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<list>
  <dict>
    <Location>None</Location>
    <XOffset>0</XOffset>
    <YOffset>0</YOffset>
  </dict>
</list>

並使用此代碼通過 jquery 閱讀此內容

$.post("demo.xml",{},function(xml){
    $('dict',xml).each(function(i) {
    location = $(this).find("Location").text();
        XOffset = $(this).find("XOffset").text();
        YOffset= $(this).find("XOffset").text();

    });

但知道我想創建這樣的 xml 文件

<key>Location</key>
<string>None</string>
<key>XOffset</key>
<string>0</string>
<key>YOffset</key>
<string>0</string>

任何人都知道我如何在 php dom 中寫這個,然后我如何讀回它

<?php

if(!file_exists("book.xml")) {
    $books = $xml->createElement("books");
     
    $newbook = $xml->createElement("book");
    $newbook->appendChild($xml->createElement("title",$_POST['title']));
    $newbook->appendChild($xml->createElement("author",$_POST['author']));
    $newbook->appendChild($xml->createElement("year",$_POST['year']));
    $newbook->appendChild($xml->createElement("pricee",$_POST['pricee']));

    $books->appendChild($newbook);
    $xml->appendChild($books);
} else {
    // load the xml file
    $xml->load("book.xml");
    $books = $xml->documentElement;

    $newbook = $xml->createElement("book");
    $newbook->appendChild($xml->createElement("title",$_POST['title']));
    $newbook->appendChild($xml->createElement("author",$_POST['author']));
    $newbook->appendChild($xml->createElement("year",$_POST['year']));
    $newbook->appendChild($xml->createElement("pricee",$_POST['pricee']));

    $books->appendChild($newbook);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM