2013-03-28 14 views
1

我目前正在研究一個新想法,以便通過PHP解析Tomboy/gnote筆記並顯示在網頁上以便添加/編輯/查看筆記。有了這樣的工具,人們就可以創建一個OwnCloud應用程序,因此可以在所有機器之間實現sincronization(包括離線訪問)幷包含一個Web UI。在後期階段,還可以開發Android應用程序。PHP XML解析(專門爲Tomboy/Gnote解析以在Web UI上顯示/編輯/添加)

我對PHP和XML有點新,因此需要一些幫助。

下面是一個示例XML文件:

<?xml version="1.0"?> 
<note version="0.3" xmlns:link="http://beatniksoftware.com/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size" xmlns="http://beatniksoftware.com/tomboy"> 
<title>TEST Note</title><text xml:space="preserve"> 
<note-content version="0.1" xmlns:link="http://beatniksoftware.com/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">TEST Note 

Normal text 
<size:huge>Huge text</size:huge> 
<size:large>Large text</size:large> 
<size:small>small text</size:small> 
<bold>BOLD Text</bold> 
<italic>Italic text</italic> 
<strikethrough>Striked text</strikethrough> 
<highlight>Highlight text</highlight> 
<list><list-item dir="ltr">BulletA 
<list><list-item dir="ltr">A1 
</list-item><list-item dir="ltr">A2 
<list><list-item dir="ltr">A2.1 
<list><list-item dir="ltr">A2.1.1 
<list><list-item dir="ltr">A2.1.1.1 
<list><list-item dir="ltr">A2.1.1.1.1 
</list-item></list></list-item></list></list-item><list-item dir="ltr">A2.1.2 
</list-item></list></list-item><list-item dir="ltr">A2.2 
</list-item></list></list-item><list-item dir="ltr">A3 
</list-item></list></list-item><list-item dir="ltr">BulletB</list-item></list> 

Normal text with space before 

And again</note-content> 
</text><last-change-date>2013-03-28T02:11:04.603700Z</last-change-date> 
<last-metadata-change-date>2013-03-28T02:11:11.012211Z</last-metadata-change-date><create-date>2013-03-28T01:39:08.607520Z</create-date> 
<cursor-position>124</cursor-position><selection-bound-position>124</selection-bound-position><width>496</width> 
<height>458</height><x>0</x><y>0</y> 
<tags><tag>system:notebook:test</tag></tags><open-on-startup>False</open-on-startup></note> 

這裏是我目前擁有的代碼:

<?php 

$file = "example.note"; 

$xmlFile = file_get_contents($file) or die('Cant open note'); 
$xml = simplexml_load_string($xmlFile); 
print_r($xml); 

?> 

這裏是輸出:

SimpleXMLElement Object ([@attributes] => Array ( 
[version] => 0.3) 
[title] => TEST Note 
[text] => SimpleXMLElement Object ( 
[note-content] => TEST Note Normal text Normal text with space before And again) 
[last-change-date] => 2013-03-28T02:11:04.603700Z 
[last-metadata-change-date] => 2013-03-28T02:11:11.012211Z 
[create-date] => 2013-03-28T01:39:08.607520Z 
[cursor-position] => 124 
[selection-bound-position] => 124 
[width] => 496 [height] => 458 
[x] => 0 [y] => 0 
[tags] => SimpleXMLElement Object ([tag] => system:notebook:test) [open-on-startup] => False) 

正如你所看到的,「筆記內容」並沒有被完全收集,所以我正在接觸PHP大師的。我怎樣才能得到整個XML解析爲了能夠然後在正常的HTML顯示它?

感謝,

回答

1

你真的需要遍歷樹和每一個元素去了在<note-content>print_r不是真正的獲得準確輸出的手段,也不是它的功能。

對於這樣的事情,我會強烈推薦使用XMLReader。它對於像這樣鬆散格式化的xml非常適合。

最後需要做的是遍歷每個元素,並且(可能)將標記(如<bold>)更改爲html <strong>標記。 Simplexml會爲此目的而吸引人。