gpt4 book ai didi

xml - 使用 QML XmlListModel 解析 XML

转载 作者:数据小太阳 更新时间:2023-10-29 01:47:24 27 4
gpt4 key购买 nike

我在使用 XmlListModel 解析 xml 文件时遇到问题,这是 XML 文件

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:searchResponse xmlns:ns1="http://tempuri.org">
<return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="unnamed_struct_use_soapval[10]">
<item>
<title xsi:type="xsd:string">Es Messi deportista del año en Israel</title>
<excerpt xsi:type="xsd:string">El grupo editorial.............</excerpt>
<img xsi:type="xsd:string">http://www.noticiasmvs.com/media/fotos/9edd0407a7bd0a47df3ece7869678c4a.jpg</img>
<url xsi:type="xsd:string">http://www.noticiasmvs.com/xmlapp/article.php?noticia_id=52872</url>
<pubdate xsi:type="xsd:string">miércoles 28 de diciembre de 2011, 03:01:54</pubdate>
</item>
<item>
<title xsi:type="xsd:string">Primera ronda de los playoffs NBA</title>
<excerpt xsi:type="xsd:string">Resultados de los partidos ya disputados y programa de los que restan por jugar de la primera ronda de los playoffs de la NBA</excerpt>
<img xsi:type="xsd:string"> http://www.noticiasmvs.com/media/fotos/7b9e23b87817132e9a1771cad7de819d.jpg</img>
<url xsi:type="xsd:string">http://www.noticiasmvs.com/xmlapp/article.php?noticia_id=4982</url>
<pubdate xsi:type="xsd:string">viernes 30 de abril de 2010, 01:27:17</pubdate>
</item>
</return>
</ns1:searchResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我用这个 XmlListModel 解析它

XmlListModel {
query: "/SOAP-ENV/SOAP-ENV/ns1/return/item"

id:searchParser
XmlRole{ name: "title"; query: "title/string()"}
XmlRole{ name: "excerpt"; query: "excerpt/string()"}
XmlRole{ name: "img"; query: "img/string()"}
XmlRole{ name: "url"; query: "url/string()"}
XmlRole{ name: "pubdate"; query: "pubdate/string()"}

最后它返回状态就绪但模型中没有任何元素,我做错了什么??

最佳答案

您的查询字符串不正确:您必须编写包含其 namespace 的元素名称。 XPath 表达式应如下所示:

/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:searchResponse/return/item

要在查询中使用命名空间,您必须像在 XQuery 中一样声明它们。这里有一个例子:

import QtQuick 1.1

Rectangle {
XmlListModel {
id: searchParser

source: "data.xml" // contains XML content
query: "/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:searchResponse/return/item"

namespaceDeclarations: "declare namespace SOAP-ENV = 'http://schemas.xmlsoap.org/soap/envelope/';
declare namespace ns1 = 'http://tempuri.org';"

XmlRole { name: "title"; query: "title/string()" }
XmlRole { name: "excerpt"; query: "excerpt/string()" }
XmlRole { name: "img"; query: "img/string()" }
XmlRole { name: "url"; query: "url/string()" }
XmlRole { name: "pubdate"; query: "pubdate/string()" }

onCountChanged: console.log("count:", count)
}
}

关于xml - 使用 QML XmlListModel 解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8670927/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com