----- isHidden: false menupriority: 1 kind: article created_at: 2010-05-19T22:20:34+02:00 title: Comment réparer un XML coupé ? subtitle: et comment s'en sortir sans parseur ? author_name: Yann Esposito author_uri: yannesposito.com tags: - arbre - HTML - script - ruby ----- Sur ma page d'accueil vous pouvez voir la liste des mes derniers articles avec le début de ceux-ci. Pour arriver à faire ça, j'ai besoin de couper le code XHTML de mes pages en plein milieu. Il m'a donc fallu trouver un moyen de les réparer. Prenons un exemple :

Introduction

The first paragraph

an image

Another long paragraph

Après avoir coupé, j'obtiens :

Introduction

The first paragraph

[] [div]
[div, div]
[div, div, p]

Introduction [div, div]

[div]
[div, p]

The first paragraph [div]

[div] an image [div, p]

Another long paragraph [div]

[]
L'algorithme est alors très simple : let res be the XML as a string ; read res and each time you encouter a tag: if it is an opening one: push it to the stack else if it is a closing one: pop the stack. remove any malformed/cutted tag in the end of res for each tag in the stack, pop it, and write: res = res + closed tag return res Et `res` contiend le XML réparé. Finallement, voici le code en ruby que j'utilise. La variable `xml` contient le XML coupé. # repair cutted XML code by closing the tags # work even if the XML is cut into a tag. # example: # transform '
toto

hello ]*$/m,'') depth-=1 depth.downto(0).each { |x| res<<= %{} } res end Je ne sais pas si ce code pourra vous être utile. Par contre le raisonnement pour y parvenir mérite d'être connu.