Konversi elemen dom menjadi string php

Saat menyimpan data, data harus dalam format tertentu, dan di mana pun Anda memilih untuk menyimpannya, teks selalu menjadi salah satu format yang sah

JSON memungkinkan untuk menyimpan objek JavaScript sebagai teks

Contoh

Menyimpan data di penyimpanan lokal

// Menyimpan data
const myObj = {nama. "Yohanes", usia. 31, kota. "New York"};
const myJSON = JSON. stringify(myObj);
penyimpanan lokal. setItem("testJSON", myJSON);

// Mengambil data
biarkan teks = localStorage. getItem("testJSON");
biarkan obj = JSON. parse(teks);
dokumen. getElementById("demo"). innerHTML = obj. nama;

Cobalah sendiri "



Pengecualian

Merangkai Tanggal

Di JSON, objek tanggal tidak diperbolehkan. Fungsi JSON.stringify() akan mengonversi tanggal apa pun menjadi string

Contoh

const obj = {nama. "Yohanes", hari ini. Tanggal baru(), kota. "New York"};
const myJSON = JSON. merangkai(obj);

Cobalah sendiri "

Anda dapat mengubah string kembali menjadi objek tanggal di penerima


Merangkai Fungsi

Di JSON, fungsi tidak diperbolehkan sebagai nilai objek

Fungsi JSON.stringify() akan menghapus semua fungsi dari objek JavaScript, baik kunci maupun nilainya

Contoh

const obj = {nama. "Yohanes", usia. fungsi () {kembali 30;}, kota. "New York"};
const myJSON = JSON. merangkai(obj);

Cobalah sendiri "

Ini dapat dihilangkan jika Anda mengubah fungsi menjadi string sebelum menjalankan fungsi JSON.stringify()

Contoh

const obj = {nama. "Yohanes", usia. fungsi () {kembali 30;}, kota. "New York"};
obj. umur = obj. usia. toString();
const myJSON = JSON. merangkai(obj);

Cobalah sendiri "

Jika Anda mengirim fungsi menggunakan JSON, fungsi tersebut akan kehilangan cakupannya, dan penerima harus menggunakan eval() untuk mengubahnya kembali menjadi fungsi

Elemen DOME. Fungsi __construct() adalah fungsi bawaan di PHP yang digunakan untuk membuat objek DOMElement baru. Objek ini hanya-baca dan dapat ditambahkan ke dokumen, tetapi simpul tambahan tidak boleh ditambahkan ke simpul ini sampai simpul dikaitkan dengan dokumen

Comment to `devin at SPAMISBAD dot tritarget dot com''s post:

Thanks for pointing out the pitfalls of `formatOutput' vs. `load*()'. This has certainly saved me from some possible surprises._

I think the seemingly strange behaviour can be explained. Warning: The following stuff is mostly based on deductions and experiments. Much less on studying the sources and specs (I'm not sure some of these would provide answer anyway, at least not easily).

As you point out, `preserveWhiteSpace' must be set before loading the DOM from the source string (I'm working with `loadXML()' but I believe the situation should be the same with `load()' you used). This looks logical, as this property seems to control the parsing and DOM creation process during which text nodes containing the whitespace are either included or dropped. This can be proven by dumping the DOM structure and comparing the results based on the value of `preserveWhiteSpace'. With `preserveWhiteSpace' set to `FALSE', no text nodes containing whitespace will be present in the returned DOM. When this property is `TRUE', these nodes will be present._

Note: When speaking about the whitespace in the previous paragraph, we're most certainly speaking about so called `whitespace in element content' or `element content whitespace', if I'm not mistaken. See also my comment in the notes of `DOMText->isWhitespaceInElementContent()' method.

As for the mysterious effect on the output of `saveXLM()', I think the explanation lies in the presence or absence of the above mentioned whitespace text nodes. This was also proven by experiments: After adding such a node into a DOM which contained none (the DOM was created using `loadXML()' with `preserveWhiteSpace' set to `FALSE'), the output formatting got affected in a such a way, the formatting got lost for the rest of the document after the added node. I think the presence of whitespace text nodes forces such rendering, that the content of these nodes is used to separate adjoining nodes thus disabling default formatting. Only when there are no such text nodes present, the ouput formatting takes effect (provided the `formatOutput' is set to `TRUE', of course)._

Well, the thing I don't really understand is how you did get an output of a signle line with `formatOutput' set to `TRUE'. This has happened to me when no whitespace text nodes were present (ie. when loading the XML with `preserveWhiteSpace' set to `FALSE') *and* with `formatOutput' set to *`FALSE'* (with the opposite value of `formatOutput', the formatting should do it's work and you should not end up with just one line). But I haven't seen your source. Perhaps you had whitespace nodes containing no new-lines in your DOM?_

As for the CAUTION about root element, I didn't see any problems with empty root element neither in shortened nor full form. What did you have in mind, when you said it `WORKS' or `DOES NOT WORK'?

Metode mana yang mengubah objek DOMDocument menjadi string?

DOMDocument mengimplementasikan DOM API - mengakses node melalui hal-hal seperti getElementById(), getElementsByClassName() , dll, dan menggunakan saveXML() untuk .

Bagaimana cara mengakses elemen DOM di PHP?

Dokumen DOM. fungsi getElementById() adalah fungsi bawaan di PHP yang digunakan untuk mencari elemen dengan id tertentu. Parameter. Fungsi ini menerima parameter tunggal $elementId yang menyimpan id untuk dicari. Nilai Pengembalian. Fungsi ini mengembalikan DOMElement atau NULL jika elemen tidak ditemukan.

Apa itu DOMDocument() di PHP?

Dokumen DOM. fungsi getElementsByTagName() adalah fungsi bawaan dalam PHP yang digunakan untuk mengembalikan turunan baru dari kelas DOMNodeList yang berisi semua elemen nama tag lokal .

Bisakah kita menggunakan DOM di PHP?

Ekstensi PHP DOM digunakan untuk beroperasi pada dokumen XML menggunakan DOM API . Ekstensi DOM menggunakan pengkodean UTF-8.