Làm cách nào để chuyển đổi tệp PHP sang định dạng XML?

________số 8

Most simply, because the whole point of SimpleXML is to be easier to use and more powerful than a plain array. For instance, you can write bar->baz['bing'] ?> and it means the same thing as bar[0]->baz[0]['bing'] ?>, regardless of how many bar or baz elements there are in the XML; and if you write bar[0]->baz[0] ?> you get all the string content of that node - including CDATA sections - regardless of whether it also has child elements or attributes. You also have access to namespace information, the ability to make simple edits to the XML, and even the ability to "import" into a DOM object, for much more powerful manipulation. All of this is lost by turning the object into an array rather than reading understanding the examples on this page.

$csv_string ='policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity
119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1
448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3
206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1
333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3
172534,FL,CLAY COUNTY,0,254281.5,0,254281.5,254281.5,246144.49,0,0,0,0,30.060614,-81.702675,Residential,Wood,1
785275,FL,CLAY COUNTY,0,515035.62,0,0,515035.62,884419.17,0,0,0,0,30.063236,-81.707703,Residential,Masonry,3
995932,FL,CLAY COUNTY,0,19260000,0,0,19260000,20610000,0,0,0,0,30.102226,-81.713882,Commercial,Reinforced Concrete,1
223488,FL,CLAY COUNTY,328500,328500,328500,328500,328500,348374.25,0,16425,0,0,30.102217,-81.707146,Residential,Wood,1
433512,FL,CLAY COUNTY,315000,315000,315000,315000,315000,265821.57,0,15750,0,0,30.118774,-81.704613,Residential,Wood,1';
0

$csv_string ='policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity
119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1
448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3
206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1
333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3
172534,FL,CLAY COUNTY,0,254281.5,0,254281.5,254281.5,246144.49,0,0,0,0,30.060614,-81.702675,Residential,Wood,1
785275,FL,CLAY COUNTY,0,515035.62,0,0,515035.62,884419.17,0,0,0,0,30.063236,-81.707703,Residential,Masonry,3
995932,FL,CLAY COUNTY,0,19260000,0,0,19260000,20610000,0,0,0,0,30.102226,-81.713882,Commercial,Reinforced Concrete,1
223488,FL,CLAY COUNTY,328500,328500,328500,328500,328500,348374.25,0,16425,0,0,30.102217,-81.707146,Residential,Wood,1
433512,FL,CLAY COUNTY,315000,315000,315000,315000,315000,265821.57,0,15750,0,0,30.118774,-81.704613,Residential,Wood,1';
1

Ví dụ, chuyển đổi csv sang xml bằng PHP, hiển thị chuyển đổi dữ liệu csv sang dữ liệu xml. Như tiêu đề gợi ý chuyển đổi csv sang xml bằng PHP, vì vậy tôi sẽ chuyển đổi tệp csv thành tệp xml hoặc chuỗi csv thành chuỗi xml. Khi tôi chuyển đổi chuỗi csv thành chuỗi xml, bạn sẽ thấy đầu ra trên trình duyệt và khi tôi chuyển đổi tệp csv thành tệp xml thì bạn sẽ thấy tài liệu xml được lưu vào một tệp trên vị trí ổ đĩa vật lý. Vì vậy, ở đây trong ví dụ này, bạn sẽ thấy cả hai ví dụ có thể giúp bạn chọn ví dụ theo tình huống của mình

Những người làm việc với các loại dữ liệu khác nhau có thể gặp phải vấn đề là họ cần chuyển đổi giữa các định dạng dữ liệu khác nhau. Các giá trị được phân tách bằng dấu phẩy (CSV) và Ngôn ngữ đánh dấu có thể mở rộng (XML) là các định dạng dữ liệu được sử dụng rộng rãi nhất và việc chuyển đổi giữa hai định dạng này cần phải được thực hiện thường xuyên. Đặc biệt là với XML, vì định dạng này được hỗ trợ rất tốt bởi các ứng dụng hiện đại và rất phù hợp để thao tác và tùy chỉnh dữ liệu sau này

Có rất nhiều công cụ có sẵn trực tuyến để chuyển đổi csv sang xml nhưng rất khó để tìm ra các phương pháp thích hợp phù hợp với nhu cầu cụ thể của một người. Vì vậy, ở đây tôi sẽ viết chương trình của riêng mình để chuyển đổi csv sang xml bằng ngôn ngữ php. Do đó, bạn không cần phải phụ thuộc vào bất kỳ công cụ cụ thể nào như vậy để chuyển đổi tài liệu csv sang xml

bài viết liên quan

  • Cách chuyển đổi XML sang CSV bằng PHP
  • Đọc và ghi vào tệp CSV bằng PHP

Vì vậy, hãy bắt đầu vào phần thực hiện

điều kiện tiên quyết

PHP7. 3. 5 – 7. 4. 3, Apache 2. 4 Máy chủ HTTP

thư mục dự án

Giả định rằng bạn đã thiết lập Máy chủ HTTP PHP và Apache trong hệ điều hành của mình

Bây giờ, tôi sẽ tạo một thư mục gốc của dự án có tên là php-csv-xml ở bất kỳ đâu trong hệ thống và tất cả các tệp sẽ được lưu trong thư mục gốc này

Tôi có thể không đề cập đến thư mục gốc của dự án trong các phần tiếp theo và tôi sẽ cho rằng tôi đang nói về thư mục gốc của dự án

Chuyển đổi chuỗi CSV sang XML

Tôi sẽ viết mã PHP bên dưới để chuyển đổi csv thành chuỗi xml. Ở đây mình sẽ lấy chuỗi csv làm đầu vào và hiển thị chuỗi xml làm đầu ra trên trình duyệt

//function to convert csv to xml string
function convertCsvToXmlString($csv_string) {
	// split rows into lines
	$lines = explode(PHP_EOL, $csv_string);
	
	// retrieve headers
	$headers = explode(',', array_shift($lines));
	
	// Create a new dom document with pretty formatting
	$doc  = new DomDocument();
	$doc->formatOutput   = true;
	
	// Add a root node to the document
	$root = $doc->createElement('policies');
	$root = $doc->appendChild($root);
	
	// Loop through each row creating a  node with the correct data
	foreach ($lines as $line)
	{
		$row = str_getcsv($line);
		$container = $doc->createElement('policy');
		
		foreach($headers as $i => $header)
		{
			$child = $doc->createElement($header);
			$child = $container->appendChild($child);
			$value = $doc->createTextNode($row[$i]);
			$value = $child->appendChild($value);
		}
		$root->appendChild($container);
	}
	
	$strxml = $doc->saveXML();
	
	echo $strxml;
}

Ở hàm trên, mình lấy tất cả các dòng vào biến

$csv_string ='policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity
119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1
448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3
206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1
333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3
172534,FL,CLAY COUNTY,0,254281.5,0,254281.5,254281.5,246144.49,0,0,0,0,30.060614,-81.702675,Residential,Wood,1
785275,FL,CLAY COUNTY,0,515035.62,0,0,515035.62,884419.17,0,0,0,0,30.063236,-81.707703,Residential,Masonry,3
995932,FL,CLAY COUNTY,0,19260000,0,0,19260000,20610000,0,0,0,0,30.102226,-81.713882,Commercial,Reinforced Concrete,1
223488,FL,CLAY COUNTY,328500,328500,328500,328500,328500,348374.25,0,16425,0,0,30.102217,-81.707146,Residential,Wood,1
433512,FL,CLAY COUNTY,315000,315000,315000,315000,315000,265821.57,0,15750,0,0,30.118774,-81.704613,Residential,Wood,1';
2 bằng dấu phân cách
$csv_string ='policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity
119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1
448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3
206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1
333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3
172534,FL,CLAY COUNTY,0,254281.5,0,254281.5,254281.5,246144.49,0,0,0,0,30.060614,-81.702675,Residential,Wood,1
785275,FL,CLAY COUNTY,0,515035.62,0,0,515035.62,884419.17,0,0,0,0,30.063236,-81.707703,Residential,Masonry,3
995932,FL,CLAY COUNTY,0,19260000,0,0,19260000,20610000,0,0,0,0,30.102226,-81.713882,Commercial,Reinforced Concrete,1
223488,FL,CLAY COUNTY,328500,328500,328500,328500,328500,348374.25,0,16425,0,0,30.102217,-81.707146,Residential,Wood,1
433512,FL,CLAY COUNTY,315000,315000,315000,315000,315000,265821.57,0,15750,0,0,30.118774,-81.704613,Residential,Wood,1';
3 (End Of Line delimiter)

Dòng tiếp theo lấy tất cả các tiêu đề từ chuỗi csv vào biến

$csv_string ='policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity
119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1
448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3
206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1
333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3
172534,FL,CLAY COUNTY,0,254281.5,0,254281.5,254281.5,246144.49,0,0,0,0,30.060614,-81.702675,Residential,Wood,1
785275,FL,CLAY COUNTY,0,515035.62,0,0,515035.62,884419.17,0,0,0,0,30.063236,-81.707703,Residential,Masonry,3
995932,FL,CLAY COUNTY,0,19260000,0,0,19260000,20610000,0,0,0,0,30.102226,-81.713882,Commercial,Reinforced Concrete,1
223488,FL,CLAY COUNTY,328500,328500,328500,328500,328500,348374.25,0,16425,0,0,30.102217,-81.707146,Residential,Wood,1
433512,FL,CLAY COUNTY,315000,315000,315000,315000,315000,265821.57,0,15750,0,0,30.118774,-81.704613,Residential,Wood,1';
4

Tiếp theo tôi muốn tạo chuỗi xml với định dạng đẹp. Sau đó, tôi tạo các chính sách nút gốc cho chuỗi xml. Bạn cũng có thể đặt tên khác, theo sự lựa chọn hoặc yêu cầu của bạn

Sau đó, tôi lặp qua từng dòng và tạo chính sách nút trong chính sách nút gốc. Đối với mỗi nút chính sách, tôi tạo các nút có tiêu đề và đặt giá trị cho từng nút trong nút chính sách

Thử nghiệm Chuyển đổi chuỗi CSV sang XML

Bây giờ mình sẽ test chức năng trên bằng code php. Tôi gán chuỗi csv với dữ liệu mẫu thành một biến như hình bên dưới

$csv_string ='policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity
119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1
448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3
206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1
333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3
172534,FL,CLAY COUNTY,0,254281.5,0,254281.5,254281.5,246144.49,0,0,0,0,30.060614,-81.702675,Residential,Wood,1
785275,FL,CLAY COUNTY,0,515035.62,0,0,515035.62,884419.17,0,0,0,0,30.063236,-81.707703,Residential,Masonry,3
995932,FL,CLAY COUNTY,0,19260000,0,0,19260000,20610000,0,0,0,0,30.102226,-81.713882,Commercial,Reinforced Concrete,1
223488,FL,CLAY COUNTY,328500,328500,328500,328500,328500,348374.25,0,16425,0,0,30.102217,-81.707146,Residential,Wood,1
433512,FL,CLAY COUNTY,315000,315000,315000,315000,315000,265821.57,0,15750,0,0,30.118774,-81.704613,Residential,Wood,1';

Sau đó, tôi đặt tiêu đề bằng

$csv_string ='policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity
119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1
448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3
206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1
333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3
172534,FL,CLAY COUNTY,0,254281.5,0,254281.5,254281.5,246144.49,0,0,0,0,30.060614,-81.702675,Residential,Wood,1
785275,FL,CLAY COUNTY,0,515035.62,0,0,515035.62,884419.17,0,0,0,0,30.063236,-81.707703,Residential,Masonry,3
995932,FL,CLAY COUNTY,0,19260000,0,0,19260000,20610000,0,0,0,0,30.102226,-81.713882,Commercial,Reinforced Concrete,1
223488,FL,CLAY COUNTY,328500,328500,328500,328500,328500,348374.25,0,16425,0,0,30.102217,-81.707146,Residential,Wood,1
433512,FL,CLAY COUNTY,315000,315000,315000,315000,315000,265821.57,0,15750,0,0,30.118774,-81.704613,Residential,Wood,1';
5 vì tôi muốn hiển thị dữ liệu xml trên trình duyệt

header('Content-type: text/xml');

Tiếp theo tôi gọi hàm trên để chuyển đổi chuỗi CSV thành chuỗi XML

convertCsvToXmlString($csv_string);

Bây giờ khi bạn chạy tệp php, bạn sẽ thấy đầu ra xml trên trình duyệt như trong hình bên dưới

Làm cách nào để chuyển đổi tệp PHP sang định dạng XML?

Như vậy bạn đã xem cách chuyển csv sang xml bằng php. Ở đây tôi đã sử dụng chuỗi csv

Chuyển đổi tệp CSV sang XML

Tiếp theo, bạn sẽ thấy cách đọc tệp csv và tạo tệp xml dưới dạng đầu ra

Bạn cũng có thể muốn đọc Đọc từ và ghi vào tệp csv bằng cách sử dụng php

Vì vậy, hãy tạo hàm php bên dưới để chuyển đổi tệp csv thành tệp xml

function convertCsvToXmlFile($input_file, $output_file) {
	// Open csv file for reading
	$inputFile  = fopen($input_file, 'rt');
	
	// Get the headers of the file
	$headers = fgetcsv($inputFile);
	
	// Create a new dom document with pretty formatting
	$doc  = new DomDocument();
	$doc->formatOutput   = true;
	
	// Add a root node to the document
	$root = $doc->createElement('policies');
	$root = $doc->appendChild($root);
	
	// Loop through each row creating a  node with the correct data
	while (($row = fgetcsv($inputFile)) !== FALSE)
	{
		$container = $doc->createElement('policy');
		
		foreach($headers as $i => $header)
		{
			$child = $doc->createElement($header);
			$child = $container->appendChild($child);
			$value = $doc->createTextNode($row[$i]);
			$value = $child->appendChild($value);
		}
		$root->appendChild($container);
	}
	
	$strxml = $doc->saveXML();
	
	$handle = fopen($output_file, "w");
	fwrite($handle, $strxml);
	fclose($handle);
}

Trong chức năng trên,

$csv_string ='policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity
119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1
448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3
206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1
333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3
172534,FL,CLAY COUNTY,0,254281.5,0,254281.5,254281.5,246144.49,0,0,0,0,30.060614,-81.702675,Residential,Wood,1
785275,FL,CLAY COUNTY,0,515035.62,0,0,515035.62,884419.17,0,0,0,0,30.063236,-81.707703,Residential,Masonry,3
995932,FL,CLAY COUNTY,0,19260000,0,0,19260000,20610000,0,0,0,0,30.102226,-81.713882,Commercial,Reinforced Concrete,1
223488,FL,CLAY COUNTY,328500,328500,328500,328500,328500,348374.25,0,16425,0,0,30.102217,-81.707146,Residential,Wood,1
433512,FL,CLAY COUNTY,315000,315000,315000,315000,315000,265821.57,0,15750,0,0,30.118774,-81.704613,Residential,Wood,1';
0 đề cập đến đường dẫn tệp csv hoàn chỉnh với tên tệp.
$csv_string ='policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity
119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1
448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3
206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1
333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3
172534,FL,CLAY COUNTY,0,254281.5,0,254281.5,254281.5,246144.49,0,0,0,0,30.060614,-81.702675,Residential,Wood,1
785275,FL,CLAY COUNTY,0,515035.62,0,0,515035.62,884419.17,0,0,0,0,30.063236,-81.707703,Residential,Masonry,3
995932,FL,CLAY COUNTY,0,19260000,0,0,19260000,20610000,0,0,0,0,30.102226,-81.713882,Commercial,Reinforced Concrete,1
223488,FL,CLAY COUNTY,328500,328500,328500,328500,328500,348374.25,0,16425,0,0,30.102217,-81.707146,Residential,Wood,1
433512,FL,CLAY COUNTY,315000,315000,315000,315000,315000,265821.57,0,15750,0,0,30.118774,-81.704613,Residential,Wood,1';
1 đề cập đến đường dẫn tệp đầu ra xml hoàn chỉnh với tên tệp

Đầu tiên tôi đọc tệp csv ở chế độ văn bản (rt. r -> đọc, t -> chế độ văn bản). Sau đó, tôi truy xuất các tiêu đề từ tệp csv vào biến

$csv_string ='policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity
119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1
448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3
206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1
333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3
172534,FL,CLAY COUNTY,0,254281.5,0,254281.5,254281.5,246144.49,0,0,0,0,30.060614,-81.702675,Residential,Wood,1
785275,FL,CLAY COUNTY,0,515035.62,0,0,515035.62,884419.17,0,0,0,0,30.063236,-81.707703,Residential,Masonry,3
995932,FL,CLAY COUNTY,0,19260000,0,0,19260000,20610000,0,0,0,0,30.102226,-81.713882,Commercial,Reinforced Concrete,1
223488,FL,CLAY COUNTY,328500,328500,328500,328500,328500,348374.25,0,16425,0,0,30.102217,-81.707146,Residential,Wood,1
433512,FL,CLAY COUNTY,315000,315000,315000,315000,315000,265821.57,0,15750,0,0,30.118774,-81.704613,Residential,Wood,1';
4

Sau đó, logic để tạo nút gốc và nút phụ giống nhau

Cuối cùng, tôi lưu đầu ra xml vào tệp xml. Tệp đầu ra được mở ở chế độ ghi (w)

Bây giờ tôi sẽ kiểm tra chức năng trên bằng mã php bên dưới

convertCsvToXmlFile("FL_insurance.csv", "FL_insurance.xml");

Trong đoạn mã trên FL_insurance. csv là tệp đầu vào và FL_insurance. xml là tệp đầu ra. Lưu ý rằng tệp FL_insurance. csv được lưu trong cùng thư mục nơi lưu trữ tập lệnh PHP của bạn

Thử nghiệm Chuyển đổi tệp CSV sang XML

Nội dung giống như bạn đã thấy chuỗi csv đầu vào

Bạn có thể tải xuống các tệp đầu vào và đầu ra từ liên kết bên dưới

FL_insuranceTải xuống

Đầu ra cuối cùng trong tệp xml phải tương tự như bên dưới

Làm cách nào để chuyển đổi tệp PHP sang định dạng XML?

Hy vọng bạn đã biết cách chuyển đổi csv sang xml bằng cách sử dụng php. Bạn cũng đã học cách đọc tệp csv và ghi vào tệp xml

Tôi có thể chuyển đổi PHP sang HTML không?

Tìm và chọn các tệp PHP trên máy tính của bạn rồi nhấp vào Mở để đưa chúng vào Doxillion nhằm chuyển đổi chúng sang định dạng tệp HTML . Bạn cũng có thể kéo và thả trực tiếp các tệp PHP của mình vào chương trình để chuyển đổi chúng.

Làm cách nào để chạy tệp 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. 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.

Làm cách nào để chuyển đổi HTML sang XML trực tuyến?

Từ HTML sang XML miễn phí cho phép tải URL trang web có bảng chuyển đổi sang XML. Nhấp vào nút URL, Nhập URL và Gửi . Phân tích cú pháp HTML thành XML hỗ trợ tải Tệp HTML để chuyển đổi sang XML. Nhấp vào nút Tải lên và chọn Tệp.

Làm cách nào để truy cập các phần tử XML trong PHP?

Hàm PHP simplexml_load_file() được sử dụng để đọc dữ liệu XML từ một tệp.