如何在 Flutter 中解析 xml 时使用 getElement
How to use getElement while parsing xml in Flutter
我面临无法使用 Dart 中 xml 包中的 getElement 函数的问题。我正在正确解析 xml 文档,我可以显示整个文档的字符串,但是当我使用 getElement 函数时 returns null.
这是xml文档
<?xml version="1.0" encoding="UTF-8"?>
<qti-assessment-item
xmlns="http://www.imsglobal.org/xsd/qti/imsqtiasi_v3p0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.imsglobal.org/xsd/imsqtiasi_v3p0
https://purl.imsglobal.org/spec/qti/v3p0/schema/xsd/imsqti_asiv3p0_v1p0.xsd"
identifier="firstexample"
time-dependent="false"
xml:lang="en-US">
<qti-response-declaration base-type="identifier" cardinality="single" identifier="RESPONSE">
<qti-correct-response>
<qti-value>A</qti-value>
</qti-correct-response>
</qti-response-declaration>
<qti-outcome-declaration base-type="float" cardinality="single" identifier="SCORE">
<qti-default-value>
<qti-value>A</qti-value>
</qti-default-value>
</qti-outcome-declaration>
<qti-item-body>
<p>Of the following hormones, which is produced by the adrenal glands?</p>
<qti-choice-interaction max-choices="1" min-choices="1"
response-identifier="RESPONSE">
<qti-simple-choice identifier="A">Epinephrine</qti-simple-choice>
<qti-simple-choice identifier="B">Glucagon</qti-simple-choice>
<qti-simple-choice identifier="C">Insulin</qti-simple-choice>
<qti-simple-choice identifier="D">Oxytocin</qti-simple-choice>
</qti-choice-interaction>
</qti-item-body>
<qti-response-processing
template="https://purl.imsglobal.org/spec/qti/v3p0/rptemplates/match_correct"/>
</qti-assessment-item>
用于加载 xml 文档的函数
Future<String> loadString(BuildContext context, String path) async {
return await DefaultAssetBundle.of(context).loadString(path);
}
尝试获取文档的一个元素
XmlDocument? xmlDocument;
loadString(context, "assets/qti3/items/choice-single-cardinality.xml").then(
(value) {
xmlDocument = XmlDocument.from(value);
XmlElement? response =
xmlDocument!.getElement("qti-response-declaration");
},
);
我尝试了其他 xml 文档和 xml 没有连字符的标签,但到目前为止没有任何效果..
getElement only returns a direct child element, use findAllElements to find elements deep inside the tree. See the tutorial on traversing and querying 以获得对这些访问器和许多其他访问器的描述。
我面临无法使用 Dart 中 xml 包中的 getElement 函数的问题。我正在正确解析 xml 文档,我可以显示整个文档的字符串,但是当我使用 getElement 函数时 returns null.
这是xml文档
<?xml version="1.0" encoding="UTF-8"?>
<qti-assessment-item
xmlns="http://www.imsglobal.org/xsd/qti/imsqtiasi_v3p0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.imsglobal.org/xsd/imsqtiasi_v3p0
https://purl.imsglobal.org/spec/qti/v3p0/schema/xsd/imsqti_asiv3p0_v1p0.xsd"
identifier="firstexample"
time-dependent="false"
xml:lang="en-US">
<qti-response-declaration base-type="identifier" cardinality="single" identifier="RESPONSE">
<qti-correct-response>
<qti-value>A</qti-value>
</qti-correct-response>
</qti-response-declaration>
<qti-outcome-declaration base-type="float" cardinality="single" identifier="SCORE">
<qti-default-value>
<qti-value>A</qti-value>
</qti-default-value>
</qti-outcome-declaration>
<qti-item-body>
<p>Of the following hormones, which is produced by the adrenal glands?</p>
<qti-choice-interaction max-choices="1" min-choices="1"
response-identifier="RESPONSE">
<qti-simple-choice identifier="A">Epinephrine</qti-simple-choice>
<qti-simple-choice identifier="B">Glucagon</qti-simple-choice>
<qti-simple-choice identifier="C">Insulin</qti-simple-choice>
<qti-simple-choice identifier="D">Oxytocin</qti-simple-choice>
</qti-choice-interaction>
</qti-item-body>
<qti-response-processing
template="https://purl.imsglobal.org/spec/qti/v3p0/rptemplates/match_correct"/>
</qti-assessment-item>
用于加载 xml 文档的函数
Future<String> loadString(BuildContext context, String path) async {
return await DefaultAssetBundle.of(context).loadString(path);
}
尝试获取文档的一个元素
XmlDocument? xmlDocument;
loadString(context, "assets/qti3/items/choice-single-cardinality.xml").then(
(value) {
xmlDocument = XmlDocument.from(value);
XmlElement? response =
xmlDocument!.getElement("qti-response-declaration");
},
);
我尝试了其他 xml 文档和 xml 没有连字符的标签,但到目前为止没有任何效果..
getElement only returns a direct child element, use findAllElements to find elements deep inside the tree. See the tutorial on traversing and querying 以获得对这些访问器和许多其他访问器的描述。