PHP XML解析(专门用于Tomboy/Gnote解析以在Web UI上显示/编辑/添加)


PHP XML Parse (specifically for Tomboy/Gnote parsing to display/edit/add on Web UI)

我目前正在研究一个新的想法,以便通过PHP解析假小子/gnote笔记,并在网页上显示,以便添加/编辑/查看笔记。有了这样一个工具,人们就可以创建一个OwnCloud应用程序,从而提供一个完整的解决方案,在所有机器之间同步(具有离线访问),并包括一个Web UI。在后期阶段,还可以开发Android应用程序。

我对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中显示它?

谢谢,

您确实需要遍历树并遍历<note-content>中的每个元素。print_r并不是获得准确输出的真正手段,也不是它的功能。

对于这样的事情,我实际上强烈建议使用XMLReader。对于像这样的松散格式的xml,它可以很好地工作。

最后需要做的是遍历每个元素,并且(可能)将标记(如<bold>)更改为html <strong>标记。Simplexml在这方面很糟糕。

Tomboy提供了一个XSLT文件,可以将假小子笔记转换为HTML -您可以使用它而无需编写自定义代码:

https://git.gnome.org/browse/tomboy/tree/Tomboy/Addins/ExportToHtml/ExportToHtml.xsl