function createXMLfile($booksArray){
$filePath = 'book.xml';
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('books');
for($i=0; $i<count($booksArray); $i++){
$bookId = $booksArray[$i]['id'];
$bookName = htmlspecialchars($booksArray[$i]['title']);
$bookAuthor = $booksArray[$i]['author_name'];
$bookPrice = $booksArray[$i]['price'];
$bookISBN = $booksArray[$i]['ISBN'];
$bookCategory = $booksArray[$i]['category'];
$book = $dom->createElement('book');
$book->setAttribute('id', $bookId);
$name = $dom->createElement('title', $bookName);
$book->appendChild($name);
$author = $dom->createElement('author', $bookAuthor);
$book->appendChild($author);
$price = $dom->createElement('price', $bookPrice);
$book->appendChild($price);
$isbn = $dom->createElement('ISBN', $bookISBN);
$book->appendChild($isbn);
$category = $dom->createElement('category', $bookCategory);
$book->appendChild($category);
$root->appendChild($book);
}
$dom->appendChild($root);
$dom->save($filePath);
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter