PHP XML thành chuỗi

Truyền các tệp XML quá lớn để vừa với bộ nhớ, với mức tiêu thụ bộ nhớ rất thấp. Thư viện này là sự kế thừa của XmlStreamer

Cài đặt

Hỗ trợ kế thừa

  • Tất cả các phiên bản dưới 1 đều hỗ trợ PHP 5. 3 - 7. 2
  • Phiên bản 1 trở lên hỗ trợ PHP 7. 2+

với nhà soạn nhạc

Chạy // Convenience method for creating a file streamer with the default parser $streamer = Prewk\XmlStringStreamer::createStringWalkerParser("gigantic.xml"); while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }9 để cài đặt gói này

Cách sử dụng

Giả sử bạn có một tệp XML khổng lồ 2 GB. xml chứa các mục của khách hàng giống như thế này

<?xml version="1.0" encoding="UTF-8"?> <gigantic> <customer> <firstName>Jane</firstName> <lastName>Doe</lastName> </customer> ... </gigantic>

Tạo một bộ truyền phát và phân tích nó

// Convenience method for creating a file streamer with the default parser $streamer = Prewk\XmlStringStreamer::createStringWalkerParser("gigantic.xml"); while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }

Không có phương pháp tiện lợi (tương đương về chức năng)

use Prewk\XmlStringStreamer; use Prewk\XmlStringStreamer\Stream; use Prewk\XmlStringStreamer\Parser; // Prepare our stream to be read with a 1kb buffer $stream = new Stream\File("gigantic.xml", 1024); // Construct the default parser (StringWalker) $parser = new Parser\StringWalker(); // Create the streamer $streamer = new XmlStringStreamer($parser, $stream); // Iterate through the `<customer>` nodes while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }

Phương pháp thuận tiện cho trình phân tích cú pháp UniqueNode

$streamer = Prewk\XmlStringStreamer::createUniqueNodeParser("file.xml", array("uniqueNode" => "customer"));

Trình phân tích cú pháp

Trình phân tích cú pháp\StringWalker

Hoạt động giống như một XmlReader và duyệt từng nút cây XML. Chụp theo cài đặt độ sâu nút

Trình phân tích cú pháp \ UniqueNode

Trình phân tích cú pháp nhanh hơn nhiều, ghi lại mọi thứ giữa thẻ mở và thẻ đóng của phần tử được cung cấp. Điều kiện tiên quyết đặc biệt áp dụng

nhà cung cấp luồng

Luồng\Tệp

Sử dụng nhà cung cấp này để phân tích các tệp XML lớn trên đĩa. Chọn một kích thước chunk, ví dụ. 1024 byte

$CHUNK_SIZE = 1024; $provider = new Prewk\XmlStringStreamer\Stream\File("large-xml-file.xml", $CHUNK_SIZE);

Luồng\Stdin

Sử dụng nhà cung cấp này nếu bạn muốn tạo một ứng dụng CLI truyền các tệp XML lớn thông qua STDIN

$CHUNK_SIZE = 1024; $fsp = new Prewk\XmlStringStreamer\Stream\Stdin($CHUNK_SIZE);

Luồng\Guzzle

Sử dụng nhà cung cấp này nếu bạn muốn truyền phát qua HTTP bằng Guzzle. Nằm trong repo của chính nó do yêu cầu phiên bản PHP cao hơn (5. 5). https. //github. com/prewk/xml-string-streamer-guzzle

Tùy chọn StringWalker

Cách sử dụng

use Prewk\XmlStringStreamer; use Prewk\XmlStringStreamer\Parser; use Prewk\XmlStringStreamer\Stream; $options = array( "captureDepth" => 3 ); $parser = new Parser\StringWalker($options);

Các tùy chọn khả dụng cho trình phân tích cú pháp StringWalker

OptionDefaultDescription(int) captureDepthuse Prewk\XmlStringStreamer; use Prewk\XmlStringStreamer\Stream; use Prewk\XmlStringStreamer\Parser; // Prepare our stream to be read with a 1kb buffer $stream = new Stream\File("gigantic.xml", 1024); // Construct the default parser (StringWalker) $parser = new Parser\StringWalker(); // Create the streamer $streamer = new XmlStringStreamer($parser, $stream); // Iterate through the `<customer>` nodes while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }0Depth chúng tôi bắt đầu thu thập các nút tại các thẻ (mảng)Xem ví dụCác thẻ được hỗ trợ(bool) mong đợiGTuse Prewk\XmlStringStreamer; use Prewk\XmlStringStreamer\Stream; use Prewk\XmlStringStreamer\Parser; // Prepare our stream to be read with a 1kb buffer $stream = new Stream\File("gigantic.xml", 1024); // Construct the default parser (StringWalker) $parser = new Parser\StringWalker(); // Create the streamer $streamer = new XmlStringStreamer($parser, $stream); // Iterate through the `<customer>` nodes while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }1Có hỗ trợ use Prewk\XmlStringStreamer; use Prewk\XmlStringStreamer\Stream; use Prewk\XmlStringStreamer\Parser; // Prepare our stream to be read with a 1kb buffer $stream = new Stream\File("gigantic.xml", 1024); // Construct the default parser (StringWalker) $parser = new Parser\StringWalker(); // Create the streamer $streamer = new XmlStringStreamer($parser, $stream); // Iterate through the `<customer>` nodes while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }2 trong nhận xét XML/CDATA hay không hoặc không (mảng) thẻWithAllowedGTXem ví dụNếu mong đợiGT là use Prewk\XmlStringStreamer; use Prewk\XmlStringStreamer\Stream; use Prewk\XmlStringStreamer\Parser; // Prepare our stream to be read with a 1kb buffer $stream = new Stream\File("gigantic.xml", 1024); // Construct the default parser (StringWalker) $parser = new Parser\StringWalker(); // Create the streamer $streamer = new XmlStringStreamer($parser, $stream); // Iterate through the `<customer>` nodes while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }3, tùy chọn này sẽ liệt kê các thẻ có ký tự use Prewk\XmlStringStreamer; use Prewk\XmlStringStreamer\Stream; use Prewk\XmlStringStreamer\Parser; // Prepare our stream to be read with a 1kb buffer $stream = new Stream\File("gigantic.xml", 1024); // Construct the default parser (StringWalker) $parser = new Parser\StringWalker(); // Create the streamer $streamer = new XmlStringStreamer($parser, $stream); // Iterate through the `<customer>` nodes while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }2 được phép trong đó

ví dụ

chụpĐộ sâu

Hành vi mặc định với độ sâu chụp là use Prewk\XmlStringStreamer; use Prewk\XmlStringStreamer\Stream; use Prewk\XmlStringStreamer\Parser; // Prepare our stream to be read with a 1kb buffer $stream = new Stream\File("gigantic.xml", 1024); // Construct the default parser (StringWalker) $parser = new Parser\StringWalker(); // Create the streamer $streamer = new XmlStringStreamer($parser, $stream); // Iterate through the `<customer>` nodes while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }0

<?xml version="1.0" encoding="UTF-8"?> <gigantic> <customer> <firstName>Jane</firstName> <lastName>Doe</lastName> </customer> ... </gigantic>4

sẽ nắm bắt các nút use Prewk\XmlStringStreamer; use Prewk\XmlStringStreamer\Stream; use Prewk\XmlStringStreamer\Parser; // Prepare our stream to be read with a 1kb buffer $stream = new Stream\File("gigantic.xml", 1024); // Construct the default parser (StringWalker) $parser = new Parser\StringWalker(); // Create the streamer $streamer = new XmlStringStreamer($parser, $stream); // Iterate through the `<customer>` nodes while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }6

Nhưng giả sử XML của bạn trông như thế này

<?xml version="1.0" encoding="UTF-8"?> <gigantic> <customer> <firstName>Jane</firstName> <lastName>Doe</lastName> </customer> ... </gigantic>6

Sau đó, bạn sẽ cần đặt độ sâu chụp thành use Prewk\XmlStringStreamer; use Prewk\XmlStringStreamer\Stream; use Prewk\XmlStringStreamer\Parser; // Prepare our stream to be read with a 1kb buffer $stream = new Stream\File("gigantic.xml", 1024); // Construct the default parser (StringWalker) $parser = new Parser\StringWalker(); // Create the streamer $streamer = new XmlStringStreamer($parser, $stream); // Iterate through the `<customer>` nodes while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }7 để chụp các nút use Prewk\XmlStringStreamer; use Prewk\XmlStringStreamer\Stream; use Prewk\XmlStringStreamer\Parser; // Prepare our stream to be read with a 1kb buffer $stream = new Stream\File("gigantic.xml", 1024); // Construct the default parser (StringWalker) $parser = new Parser\StringWalker(); // Create the streamer $streamer = new XmlStringStreamer($parser, $stream); // Iterate through the `<customer>` nodes while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }8

Độ sâu nút được hình dung

<?xml version="1.0" encoding="UTF-8"?> <gigantic> <customer> <firstName>Jane</firstName> <lastName>Doe</lastName> </customer> ... </gigantic>9

thẻ

Giá trị mặc định

// Convenience method for creating a file streamer with the default parser $streamer = Prewk\XmlStringStreamer::createStringWalkerParser("gigantic.xml"); while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }0

tham số đầu tiên. thẻ mở, tham số thứ hai. thẻ đóng, tham số thứ ba. chiều sâu

Nếu bạn biết rằng XML của mình không có bất kỳ nhận xét XML, CDATA hoặc thẻ tự đóng nào, bạn có thể điều chỉnh hiệu suất của mình bằng cách đặt tùy chọn thẻ và bỏ qua chúng

// Convenience method for creating a file streamer with the default parser $streamer = Prewk\XmlStringStreamer::createStringWalkerParser("gigantic.xml"); while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }1

mong đợiGT & tagsWithAllowedGT

Bạn có thể cho phép ký tự use Prewk\XmlStringStreamer; use Prewk\XmlStringStreamer\Stream; use Prewk\XmlStringStreamer\Parser; // Prepare our stream to be read with a 1kb buffer $stream = new Stream\File("gigantic.xml", 1024); // Construct the default parser (StringWalker) $parser = new Parser\StringWalker(); // Create the streamer $streamer = new XmlStringStreamer($parser, $stream); // Iterate through the `<customer>` nodes while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }2 trong phần nhận xét XML và phần CDATA nếu muốn. Điều này khá hiếm gặp và do đó bị tắt theo mặc định vì lý do hiệu suất

Giá trị mặc định cho tagsWithAllowedGT

// Convenience method for creating a file streamer with the default parser $streamer = Prewk\XmlStringStreamer::createStringWalkerParser("gigantic.xml"); while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }2

Tùy chọn UniqueNode

Cách sử dụng

// Convenience method for creating a file streamer with the default parser $streamer = Prewk\XmlStringStreamer::createStringWalkerParser("gigantic.xml"); while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }3

Các tùy chọn khả dụng cho trình phân tích cú pháp UniqueNode

Tùy chọn Mô tả (chuỗi) tùy chọn duy nhấtNodeRequired. Chỉ định tên nút để chụp (bool) checkShortClosingCó kiểm tra thẻ đóng ngắn hay không

ví dụ

duy nhấtNode

Giả sử bạn có một tệp XML như thế này

// Convenience method for creating a file streamer with the default parser $streamer = Prewk\XmlStringStreamer::createStringWalkerParser("gigantic.xml"); while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }4

Bạn muốn nắm bắt các nút nội dung, do đó, hãy đặt uniqueNode thành $streamer = Prewk\XmlStringStreamer::createUniqueNodeParser("file.xml", array("uniqueNode" => "customer"));0

Nếu bạn có một tệp XML với các thẻ đóng ngắn như thế này

// Convenience method for creating a file streamer with the default parser $streamer = Prewk\XmlStringStreamer::createStringWalkerParser("gigantic.xml"); while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }5

Bạn muốn nắm bắt các nút nội dung, do đó, hãy đặt uniqueNode thành $streamer = Prewk\XmlStringStreamer::createUniqueNodeParser("file.xml", array("uniqueNode" => "customer"));0 và checkShortClosing thành use Prewk\XmlStringStreamer; use Prewk\XmlStringStreamer\Stream; use Prewk\XmlStringStreamer\Parser; // Prepare our stream to be read with a 1kb buffer $stream = new Stream\File("gigantic.xml", 1024); // Construct the default parser (StringWalker) $parser = new Parser\StringWalker(); // Create the streamer $streamer = new XmlStringStreamer($parser, $stream); // Iterate through the `<customer>` nodes while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }3

Nhưng nếu tệp XML của bạn trông như thế này

// Convenience method for creating a file streamer with the default parser $streamer = Prewk\XmlStringStreamer::createStringWalkerParser("gigantic.xml"); while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }6

bạn sẽ không thể sử dụng trình phân tích cú pháp UniqueNode, vì $streamer = Prewk\XmlStringStreamer::createUniqueNodeParser("file.xml", array("uniqueNode" => "customer"));3 tồn tại bên trong một nút $streamer = Prewk\XmlStringStreamer::createUniqueNodeParser("file.xml", array("uniqueNode" => "customer"));3 khác

Sử dụng nâng cao

Thanh tiến trình

Bạn có thể theo dõi tiến trình bằng cách sử dụng bao đóng làm đối số thứ ba khi xây dựng lớp luồng. Ví dụ với luồng $streamer = Prewk\XmlStringStreamer::createUniqueNodeParser("file.xml", array("uniqueNode" => "customer"));5 sử dụng trình phân tích cú pháp $streamer = Prewk\XmlStringStreamer::createUniqueNodeParser("file.xml", array("uniqueNode" => "customer"));6

// Convenience method for creating a file streamer with the default parser $streamer = Prewk\XmlStringStreamer::createStringWalkerParser("gigantic.xml"); while ($node = $streamer->getNode()) { // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>" $simpleXmlNode = simplexml_load_string($node); echo (string)$simpleXmlNode->firstName; }7

Tất nhiên, bạn có thể làm điều gì đó thông minh hơn là gửi thư rác với $streamer = Prewk\XmlStringStreamer::createUniqueNodeParser("file.xml", array("uniqueNode" => "customer"));7

Truy cập phần tử gốc (phiên bản 0. 7. 0+)

Đặt tùy chọn trình phân tích cú pháp $streamer = Prewk\XmlStringStreamer::createUniqueNodeParser("file.xml", array("uniqueNode" => "customer"));8 yêu cầu trình phân tích cú pháp thu thập mọi thứ trước và sau khi chụp phần tử con dự định của bạn. Các kết quả có sẵn thông qua phương thức $streamer = Prewk\XmlStringStreamer::createUniqueNodeParser("file.xml", array("uniqueNode" => "customer"));9 của trình phân tích cú pháp

Ghi chú. $streamer = Prewk\XmlStringStreamer::createUniqueNodeParser("file.xml", array("uniqueNode" => "customer"));9 sẽ trả về những thứ khác nhau tùy thuộc vào việc bạn có phát trực tuyến toàn bộ tệp hay không. Nếu bạn cần dữ liệu XML chứa sớm, bạn có thể lấy nó bên trong vòng lặp while, nhưng nó sẽ chỉ là các phần tử mở và do đó được coi là XML không hợp lệ bởi các trình phân tích cú pháp như SimpleXML

Làm cách nào để chuyển đổi XML thành chuỗi trong PHP?

Nó chuyển đổi một chuỗi được mã hóa JSON thành một biến PHP. .
Bước 1. Tạo tệp XML (Tùy chọn). Tạo một tệp XML cần chuyển đổi thành mảng. .
Bước 2. Chuyển đổi tệp thành chuỗi. Tệp XML sẽ nhập vào PHP bằng cách sử dụng hàm file_get_contents() đọc toàn bộ tệp dưới dạng chuỗi và lưu vào một biến

Làm cách nào để đọc chuỗi XML trong PHP?

Đọc các phần tử XML cụ thể . Sau đó, bạn có thể truy cập bất kỳ phần tử nào từ XML bởi đối tượng này như sau. $xmldata = simplexml_load_file("nhân viên. xml"Use simplexml_load_file function to load external XML file in your PHP program and create an object. After that, you can access any element from the XML by this object as follows. $xmldata = simplexml_load_file("employees. xml"

Làm cách nào để chuyển đổi XML thành chuỗi trực tuyến?

Nó giúp thay thế dữ liệu XML của bạn thành văn bản XML có thể được trình bày trong HTML. Công cụ này cho phép tải URL XML, tải XML và xâu chuỗi thành văn bản. Nhấp vào nút URL, Nhập URL và Gửi. Người dùng cũng có thể chuyển đổi Tệp XML thành chuỗi XML bằng cách tải tệp lên .

Làm cách nào để chuyển đổi dữ liệu XML thành mảng trong PHP?

php // đường dẫn tệp xml $path = "abc. xml"; $xmlfile = file_get_contents($path); $new = simplexml_load_string($xmlfile); $jsonfile = json_encode($new); $myarray = json_decode($jsonfile, true); print_r($myarray); ?>

Chủ đề