10/10/2018, 10:28
Nhờ sửa giúp code php xuất ra file xml này.
Xin chào mọi người.
Em có code php này:
Khi chạy thì xuất ra file xml như sau:
Nhờ các anh chị rành về code php & mxl sửa giúp lại code php trên để xuất ra file xml này: (thêm node <booklist>)
Cam on nhieu
Em có code php này:
Code:
<?php $doc = new DOMDocument(); $doc->formatOutput = true; if(!file_exists('data.xml')){ $doc->appendChild($doc->createElement('books')); $doc->save('data.xml'); } @$doc->load('data.xml'); //Searches for all elements with the "books" tag name $root = $doc->getElementsByTagName('books'); if($root->length == 0){ $doc->appendChild($doc->createElement('books')); } /* tao doi tuong documentfragment */ $f = $doc->createDocumentFragment(); /* them item */ $item = $doc->createElement('book'); $item->appendChild($doc->createElement('name', $_POST['name'])); $item->appendChild($doc->createElement('price', $_POST['price'])); $f->appendChild($item); $doc->documentElement->appendChild($f); $doc->save('data.xml'); echo $doc->saveXML(); ?>
Code:
<?xml version="1.0"?> <books> <book> <name>aaaaaaaaaaaaa</name> <price>bbbbbbbb</price> </book> </books>
Nhờ các anh chị rành về code php & mxl sửa giúp lại code php trên để xuất ra file xml này: (thêm node <booklist>)
Code:
<?xml version="1.0"?> <books> <booklist> <book> <name>aaaaaaaaaaaaa</name> <price>bbbbbbbb</price> </book> <booklist> </books>
Bài liên quan
1. Tạo một chuỗi chứa định dạng xml
2. Ghi chuỗi ấy vào file đè lên file ban đầu
Với đoạn code php trên thì em muốn sửa như thế nào để được file xml này thôi .
top.txt
<?xml version="1.0"?>
<books>
<booklist>
body.txt
<book>
<name>aaaaaaaaaaaaa</name>
<price>bbbbbbbb</price>
</book>
<book>
<name>aaaaaaaaaaaaa1</name>
<price>bbbbbbbb1</price>
</book>
bottom.txt
<booklist>
</books>
muốn ghi thêm thì append vô cái body.txt xong gộp cả 3 cái vào thành 1 .
có được không
bạn nhìn kỹ code php chưa ?
có ai giúp nữa không vậy ???
Từ
$doc = new DOMDocument();
$doc->formatOutput = true;
if(!file_exists('data.xml')){
$doc->appendChild($doc->createElement('books'));
$doc->save('data.xml');
}
@$doc->load('data.xml');
$root = $doc->getElementsByTagName('books');
if($root->length == 0){
$doc->appendChild($doc->createElement('books'));
}
if($root[0]->childNodes[0]->nodeValue != 'booklist'){
$root[0]->appendChild($doc->createElement('booklist'));
}
/* tao doi tuong documentfragment */
$f = $doc->createDocumentFragment();
/* them item */
$item = $doc->createElement('book');
$item->appendChild($doc->createElement('name', $_POST['name']));
$item->appendChild($doc->createElement('price', $_POST['price']));
$f->appendChild($item);
$root[0]->childNodes[0]->appendChild($f);
$root[0]->save('data.xml');
echo $root[0]->saveXML();
?>
ổng chỉ cho