[請益] XML解析

看板PHP作者 (團長)時間12年前 (2013/10/14 08:59), 編輯推噓0(009)
留言9則, 5人參與, 最新討論串1/1
Say you have a set of XML files, each containing information about a book, and you want to build an index showing the document title and its author for the collection. You need to parse the XML files to recognize the title and author elements and their contents. You could do this by hand with regular expressions and string functions such as strtok(), but it’s a lot more complex than it seems. In addition, such methods are prone to breakage even with valid XML documents. The easiest and quickest solution is to use one of the XML parsers that ship with PHP. ------ 板友好,最近念到「XML parsing」章節,上方是其中一段,為閱讀方便故適當分行。 依個人解讀,前三句主要是講解析XML,可以使用regular expression或strtok函式。 到第四句就不太懂,因為依字面解讀,應該是說前述兩種做法,容易毀損XML文件。 一來不確定自己解讀是否正確,二來即使解讀正確,也不太清楚其原因為何。 還請板友多指教,謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.248.73.82

10/14 09:26, , 1F
又沒寫進去當然不會破(breakage) 當然是說讀出來會破
10/14 09:26, 1F

10/14 11:16, , 2F
大抵上就跟你不會享用 RegExp 寫 compiler 的道理一樣 XD
10/14 11:16, 2F

10/14 12:33, , 3F
簡單來說 自己造輪子搞不好會做成方的
10/14 12:33, 3F

10/14 12:34, , 4F
就算XML本身是正確的 但你規則可能有錯 所以讀到的
10/14 12:34, 4F

10/14 12:34, , 5F
內容有可能有問題 反正有現成的工具就拿來用
10/14 12:34, 5F

10/14 12:34, , 6F
除非你對寫parser有興趣 不然你不可能寫得比內建的好
10/14 12:34, 6F

10/14 13:44, , 7F
請問各位大大,如果用XML Schema會不會比較好?用.xsd
10/14 13:44, 7F

10/14 13:44, , 8F
.xsd parse .xml
10/14 13:44, 8F

10/14 23:47, , 9F
謝謝大家回應
10/14 23:47, 9F
文章代碼(AID): #1IMq7p_c (PHP)
文章代碼(AID): #1IMq7p_c (PHP)