Como analisar XML com cxml e stp e comercial contendo
-
12-12-2019 - |
Pergunta
Eu quero analisar o XML seguinte Código:
(cxml:parse "<BEGIN><URL>www.some.de/url?some=data&bad=stuff</URL></BEGIN>" (stp:make-builder))
isso resulta em
#<CXML:WELL-FORMEDNESS-VIOLATION "~A" {1003C5E163}>
como '&' é um XML de caracteres especiais.Mas se eu usar &?
em vez disso, o resultado é:
(cxml:parse "<BEGIN><URL>www.some.de/url?some=data&bad=stuff</URL></BEGIN>" (stp:make-builder))
=>#.(CXML-STP-IMPL::DOCUMENT
:CHILDREN '(#.(CXML-STP:ELEMENT
#| :PARENT of type DOCUMENT |#
:CHILDREN '(#.(CXML-STP:ELEMENT
#| :PARENT of type ELEMENT |#
:CHILDREN '(#.(CXML-STP:TEXT
#| :PARENT of type ELEMENT |#
:DATA "www.some.de/url?some=data")
#.(CXML-STP:TEXT
#| :PARENT of type ELEMENT |#
:DATA "&")
#.(CXML-STP:TEXT
#| :PARENT of type ELEMENT |#
:DATA "bad=stuff"))
:LOCAL-NAME "URL"))
:LOCAL-NAME "BEGIN")))
O que não é exatamente o que eu esperava como deve haver apenas um CXML-STP:o TEXTO criança com DADOS "www.some.de/url?some=data&bad=stuff"
Como posso corrigir isso errado(?) comportamento?
Solução
Este comportamento, embora, não muito conveniente, é, na verdade, presente em muitos outros analisadores de XML bem.Provavelmente, a razão para isso é para ser capaz de analisar XML arbitrário de entidades e aplicar algumas regras definidas pelo usuário para eles.Embora, possa ser apenas um subproduto do analisador de implementação.Eu não podia descobrir ainda.
Para o SAX variante do analisador eu vim para a seguinte abordagem:
(defclass my-sax (sax:sax-parser-mixin)
((title :accessor title :initform nil)
(tag :accessor tag :initform nil)
(text :accessor text :initform "")))
(defmethod sax:start-element ((sax my-sax) namespace-uri local-name
qname attributes)
(with-slots (tag tagcount text) sax
(setf tag local-name
text "")))
(defmethod sax:characters ((sax my-sax) data)
(with-slots (title tag text) sax
(switch (tag :test 'string=)
("text" (setf text (conatenate 'string text data)))
("title" (setf title data)))))
(defmethod sax:end-element ((sax my-sax) namespace-uri local-name qname)
(with-slots (title tag text) sax
(when (string= "text" local-name)
;; process (text sax)
)))
I. e.Eu recolher o texto em sax:characters
e processá-lo em sax:end-element
.Em STP você, provavelmente, pode sair até mais fácil, basta concatenar vizinhos text
elementos.