Home > Net >  How to use getElement while parsing xml in Flutter
How to use getElement while parsing xml in Flutter

Time:04-01

I am facing the Problem that I cant use the getElement Function from the xml Package in Dart. Im parsing the xml Document correctly and I can dsiplay the String of the whole Document but when Im using getElement the function returns null.

This is the xml Document

<?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>

function to load the xml Document

Future<String> loadString(BuildContext context, String path) async {
    return await DefaultAssetBundle.of(context).loadString(path);
  }

Try to get one element of the document

 XmlDocument? xmlDocument;
    loadString(context, "assets/qti3/items/choice-single-cardinality.xml").then(
      (value) {
        xmlDocument = XmlDocument.from(value);
        XmlElement? response =
            xmlDocument!.getElement("qti-response-declaration");
      },
    );

I tried other xml Documents and xml tags without hyphens but nothing worked so far..

CodePudding user response:

getElement only returns a direct child element, use findAllElements to find elements deep inside the tree. See the tutorial on traversing and querying for a description of these and many other accessors.

  • Related