[英]PHP creating XML is inserting extra tags and extraneous data. Help?
我正在嘗試從Excel電子表格導入XML數據(保存為'XML Spreadsheet 2003')。 PHP腳本讀取excel數據,然后將數據轉換為外部XML文件。 這個新文件反過來由javscript讀取,使用谷歌地圖API在谷歌地圖上放置標記。 我知道這是低效的,但目標是為客戶提供一個他們可以輕松編輯和上傳的電子表格,並在網站上反映這些更改,而無需我的任何干預。
問題如下:
顯示數據輸入方式的Excel屏幕截圖:
更正,沒有像我這樣的新手的圖像。 點擊屏幕截圖: 截圖
Excel XML電子表格的來源(如果我遺漏了某些內容,我將其全部包括在內):
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Brandon</Author>
<LastAuthor>Brandon</LastAuthor>
<Created>2011-04-01T21:05:56Z</Created>
<Version>14.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>7995</WindowHeight>
<WindowWidth>20115</WindowWidth>
<WindowTopX>240</WindowTopX>
<WindowTopY>75</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s62">
<Font ss:FontName="Arial" x:Family="Swiss" ss:Bold="1"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="9" ss:ExpandedRowCount="4" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="15">
<Column ss:AutoFitWidth="0" ss:Width="86.25"/>
<Column ss:AutoFitWidth="0" ss:Width="128.25"/>
<Column ss:AutoFitWidth="0" ss:Width="156.75"/>
<Column ss:AutoFitWidth="0" ss:Width="112.5"/>
<Column ss:AutoFitWidth="0" ss:Width="103.5"/>
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s62"><Data ss:Type="String">Lat</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">Long</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">Name</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">Phone</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">Address</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">City</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">State</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">Zip</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">URL</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="Number">39.769831000000003</Data></Cell>
<Cell><Data ss:Type="Number">-104.972657</Data></Cell>
<Cell><Data ss:Type="String">Generic Liquors</Data></Cell>
<Cell><Data ss:Type="String">555-123-5555</Data></Cell>
<Cell><Data ss:Type="String">42 Walnut St</Data></Cell>
<Cell><Data ss:Type="String">Denver</Data></Cell>
<Cell><Data ss:Type="String">CO</Data></Cell>
<Cell><Data ss:Type="Number">80207</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="Number">39.763903999999997</Data></Cell>
<Cell><Data ss:Type="Number">-104.966311</Data></Cell>
<Cell><Data ss:Type="String">Fancy Restaurant</Data></Cell>
<Cell><Data ss:Type="String">555-123-5556</Data></Cell>
<Cell><Data ss:Type="String">55 Colfax Drive</Data></Cell>
<Cell><Data ss:Type="String">Denver</Data></Cell>
<Cell><Data ss:Type="String">CO</Data></Cell>
<Cell><Data ss:Type="Number">80207</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="Number">39.759087000000001</Data></Cell>
<Cell><Data ss:Type="Number">-104.982963</Data></Cell>
<Cell><Data ss:Type="String">Even Fancier Restaurant</Data></Cell>
<Cell><Data ss:Type="String">555-123-5557</Data></Cell>
<Cell><Data ss:Type="String">1129 Boggio St</Data></Cell>
<Cell><Data ss:Type="String">Denver</Data></Cell>
<Cell><Data ss:Type="String">CO</Data></Cell>
<Cell><Data ss:Type="Number">87505</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Unsynced/>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>4</ActiveRow>
<ActiveCol>4</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet2">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="15">
<Row ss:AutoFitHeight="0"/>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Unsynced/>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet3">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="15">
<Row ss:AutoFitHeight="0"/>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Unsynced/>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
只有相關單元格數據的Excel XML源:
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s62"><Data ss:Type="String">Lat</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">Long</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">Name</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">Phone</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">Address</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">City</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">State</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">Zip</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="String">URL</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="Number">39.769831000000003</Data></Cell>
<Cell><Data ss:Type="Number">-104.972657</Data></Cell>
<Cell><Data ss:Type="String">Generic Liquors</Data></Cell>
<Cell><Data ss:Type="String">555-123-5555</Data></Cell>
<Cell><Data ss:Type="String">42 Walnut St</Data></Cell>
<Cell><Data ss:Type="String">Denver</Data></Cell>
<Cell><Data ss:Type="String">CO</Data></Cell>
<Cell><Data ss:Type="Number">80207</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="Number">39.763903999999997</Data></Cell>
<Cell><Data ss:Type="Number">-104.966311</Data></Cell>
<Cell><Data ss:Type="String">Fancy Restaurant</Data></Cell>
<Cell><Data ss:Type="String">555-123-5556</Data></Cell>
<Cell><Data ss:Type="String">55 Colfax Drive</Data></Cell>
<Cell><Data ss:Type="String">Denver</Data></Cell>
<Cell><Data ss:Type="String">CO</Data></Cell>
<Cell><Data ss:Type="Number">80207</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="Number">39.759087000000001</Data></Cell>
<Cell><Data ss:Type="Number">-104.982963</Data></Cell>
<Cell><Data ss:Type="String">Even Fancier Restaurant</Data></Cell>
<Cell><Data ss:Type="String">555-123-5557</Data></Cell>
<Cell><Data ss:Type="String">1129 Boggio St</Data></Cell>
<Cell><Data ss:Type="String">Denver</Data></Cell>
<Cell><Data ss:Type="String">CO</Data></Cell>
<Cell><Data ss:Type="Number">87505</Data></Cell>
</Row>
PHP腳本:
<?php
//
// Read the Excel File
$data = array();
//format the array values
function add_marker( $lat, $long, $name, $phone, $address, $city, $state, $zip, $url )
{
global $data;
$data []= array(
'Lat' => $lat,
'Long' => $long,
'Name' => $name,
'Phone' => $phone,
'Address' => $address,
'City' => $city,
'State' => $state,
'Zip' => $zip,
'URL' => $url
);
}
// Load the spreadsheet
$dom = DOMDocument::load( '/home/public/xml/tour.xml' );
// Select XML tag in the spreadsheet to use
$rows = $dom->getElementsByTagName( 'Row' );
$first_row = true;
foreach ($rows as $row)
{
if ( !$first_row )
{
$lat = "";
$long = "";
$name = "";
$phone = "";
$address = "";
$city = "";
$state = "";
$zip = "";
$url = "";
$index = 1;
$cells = $row->getElementsByTagName( 'Cell' );
foreach( $cells as $cell )
{
$ind = $cell->getAttribute( 'Index' );
if ( $ind != null ) $index = $ind;
if ( $index == 1 ) $lat = $cell->nodeValue;
if ( $index == 2 ) $long = $cell->nodeValue;
if ( $index == 3 ) $name = $cell->nodeValue;
if ( $index == 4 ) $phone = $cell->nodeValue;
if ( $index == 5 ) $address = $cell->nodeValue;
if ( $index == 6 ) $city = $cell->nodeValue;
if ( $index == 7 ) $state = $cell->nodeValue;
if ( $index == 8 ) $zip = $cell->nodeValue;
if ( $index == 9 ) $url = $cell->nodeValue;
$index += 1;
}
add_marker( $lat, $long, $name, $phone, $address, $city, $state, $zip, $url );
}
$first_row = false;
}
//
// Write and save xml file
//
//create document
$doc = new DOMDocument('1.0', 'UTF-8');
//pretty formatting
$doc->formatOutput = true;
//create 'markers' root element
$root = $doc->createElement('markers');
$doc->appendChild($root);
//run through the array constructed from excel file
foreach( $data as $row )
{
//create individual marker element
$root_child = $doc->createElement('marker');
$root->appendChild($root_child);
//set attribute of lat
$root_attr1 = $doc->createAttribute('lat');
$root_child->appendChild($root_attr1);
//assign 'lat' attribute it's value from array
$root_text = $doc->createTextNode($row['Lat']);
$root_attr1->appendChild($root_text);
//set attribute of lng
$root_attr2= $doc->createAttribute('lng');
$root_child->appendChild($root_attr2);
//assign 'lng' attribute it's value from array
$root_text = $doc->createTextNode($row['Long']);
$root_attr2->appendChild($root_text);
//set attribute of name
$root_attr3= $doc->createAttribute('name');
$root_child->appendChild($root_attr3);
//assign 'name' attribute it's value from array
$root_text = $doc->createTextNode($row['Name']);
$root_attr3->appendChild($root_text);
//set attribute of phone
$root_attr4= $doc->createAttribute('phone');
$root_child->appendChild($root_attr4);
//assign 'phone' attribute it's value from array
$root_text = $doc->createTextNode($row['Phone']);
$root_attr4->appendChild($root_text);
//set attribute of address
$root_attr5= $doc->createAttribute('address');
$root_child->appendChild($root_attr5);
//assign 'address' attribute it's value from array
$root_text = $doc->createTextNode($row['Address']);
$root_attr5->appendChild($root_text);
//set attribute of city
$root_attr6= $doc->createAttribute('city');
$root_child->appendChild($root_attr6);
//assign 'city' attribute it's value from array
$root_text = $doc->createTextNode($row['City']);
$root_attr6->appendChild($root_text);
//set attribute of state
$root_attr7= $doc->createAttribute('state');
$root_child->appendChild($root_attr7);
//assign 'state' attribute it's value from array
$root_text = $doc->createTextNode($row['State']);
$root_attr7->appendChild($root_text);
//set attribute of zip
$root_attr8= $doc->createAttribute('zip');
$root_child->appendChild($root_attr8);
//assign 'zip' attribute it's value from array
$root_text = $doc->createTextNode($row['Zip']);
$root_attr8->appendChild($root_text);
//set attribute of url
$root_attr9= $doc->createAttribute('url');
$root_child->appendChild($root_attr9);
//assign 'name' attribute it's value from array
$root_text = $doc->createTextNode($row['URL']);
$root_attr9->appendChild($root_text);
}
$doc->save("/home/public/xml/markers.xml");//save("/home/public/xml/markers.xml");
?>
輸出markers.xml文件:
<markers>
<marker lat="Lat" lng="Long" name="Name" phone="Phone" address="Address" city="City" state="State" zip="Zip" url="URL"/>
<marker lat="39.769831000000003" lng="-104.972657" name="Generic Liquors" phone="555-123-5555" address="42 Walnut St" city="Denver" state="CO" zip="80207" url="URL"/>
<marker lat="39.763903999999997" lng="-104.966311" name="Fancy Restaurant" phone="555-123-5556" address="55 Colfax Drive" city="Denver" state="CO" zip="80207" url="URL"/>
<marker lat="39.759087000000001" lng="-104.982963" name="Even Fancier Restaurant" phone="555-123-5557" address="1129 Boggio St" city="Denver" state="CO" zip="87505" url="URL"/>
<marker lat="39.759087000000001" lng="-104.982963" name="Even Fancier Restaurant" phone="555-123-5557" address="1129 Boggio St" city="Denver" state="CO" zip="87505" url="URL"/>
<marker lat="39.759087000000001" lng="-104.982963" name="Even Fancier Restaurant" phone="555-123-5557" address="1129 Boggio St" city="Denver" state="CO" zip="87505" url="URL"/>
</markers>
那里有很多額外的數字和額外的標記標簽。 想法?
問題#1:
該腳本兩次復制最后一個excel條目(創建相同數據的3 xml條目)
可能與第二個工作表中有一個空行有關。 將代碼限制為第一個工作表中的行只是因為即使行中沒有單元格,您仍然在調用add_marker()。 如果您不僅僅是第一張工作表,那么您可以這樣做
if ($index > 1)
add_marker( $lat, $long, $name, $phone, $address, $city, $state, $zip, $url );
而不僅僅是
add_marker( $lat, $long, $name, $phone, $address, $city, $state, $zip, $url );
問題#2:
包含數字的單元格在數字的中間放置了額外的零(excel這樣做)
更改提取此信息的行以將數據四舍五入到小數點后6位,例如更改
$lat = $cell->nodeValue;
至
$lat = round($cell->nodeValue,6);
問題#3:
盡管我沒有嘗試,但excel文件的標題行仍包含在XML中
確保標題實際上是第一個工作表的第1行,並且您提供的代碼應該可以正常工作。 如果標題位於第2行或更高版本中,或者如果工作簿中有任何以前的工作表,那么您將遇到此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.