使用名称空间解析xml时,DRF xml解析器无法获取嵌套列表元素(值被覆盖)

时间:2019-01-14 10:05:41

标签: xml django-rest-framework xml-parsing

我正在尝试使用Django REST XMLParser解析项目列表。解析器将项目列表视为单个项目。这是XML:

<pa-structures:Question>
    <pa-structures:QuestionID>Q1</pa-structures:QuestionID>
    <pa-structures:SequenceNumber>1</pa-structures:SequenceNumber>
    <pa-structures:QuestionText>Is this patient home self-administering?</pa-structures:QuestionText>
    <pa-structures:DefaultNextQuestionID>Q2</pa-structures:DefaultNextQuestionID>
    <pa-structures:QuestionType>
        <pa-structures:Select>
            <pa-structures:SelectMultiple>N</pa-structures:SelectMultiple>
            <pa-structures:Choice>
                <pa-structures:ChoiceID>Q1C1</pa-structures:ChoiceID>
                <pa-structures:SequenceNumber>1</pa-structures:SequenceNumber>
                <pa-structures:ChoiceText>Yes</pa-structures:ChoiceText>
                <pa-structures:AdditionalFreeTextIndicator>NA</pa-structures:AdditionalFreeTextIndicator>
            </pa-structures:Choice>
            <pa-structures:Choice>
                <pa-structures:ChoiceID>Q1C2</pa-structures:ChoiceID>
                <pa-structures:SequenceNumber>2</pa-structures:SequenceNumber>
                <pa-structures:ChoiceText>No</pa-structures:ChoiceText>
                <pa-structures:AdditionalFreeTextIndicator>NA</pa-structures:AdditionalFreeTextIndicator>
            </pa-structures:Choice>
        </pa-structures:Select>
    </pa-structures:QuestionType>
</pa-structures:Question>
<pa-structures:Question>
    <pa-structures:QuestionID>Q2</pa-structures:QuestionID>
    <pa-structures:SequenceNumber>2</pa-structures:SequenceNumber>
    <pa-structures:QuestionText>Is the medication to be used to treat prophylaxis?</pa-structures:QuestionText>
    <pa-structures:QuestionType>
        <pa-structures:Select>
            <pa-structures:SelectMultiple>N</pa-structures:SelectMultiple>
            <pa-structures:Choice>
                <pa-structures:ChoiceID>Q2C1</pa-structures:ChoiceID>
                <pa-structures:SequenceNumber>1</pa-structures:SequenceNumber>
                <pa-structures:ChoiceText>Yes</pa-structures:ChoiceText>
                <pa-structures:AdditionalFreeTextIndicator>NA</pa-structures:AdditionalFreeTextIndicator>
                <pa-structures:NextQuestionID>Q3</pa-structures:NextQuestionID>
            </pa-structures:Choice>
            <pa-structures:Choice>
                <pa-structures:ChoiceID>Q2C2</pa-structures:ChoiceID>
                <pa-structures:SequenceNumber>2</pa-structures:SequenceNumber>
                <pa-structures:ChoiceText>No</pa-structures:ChoiceText>
                <pa-structures:AdditionalFreeTextIndicator>NA</pa-structures:AdditionalFreeTextIndicator>
                <pa-structures:NextQuestionID>Q4</pa-structures:NextQuestionID>
            </pa-structures:Choice>
        </pa-structures:Select>
    </pa-structures:QuestionType>
</pa-structures:Question>
<pa-structures:Question>
    <pa-structures:QuestionID>Q3</pa-structures:QuestionID>
    <pa-structures:SequenceNumber>3</pa-structures:SequenceNumber>
    <pa-structures:QuestionText>When was or will the surgery be done?</pa-structures:QuestionText>
    <pa-structures:DefaultNextQuestionID>Q6</pa-structures:DefaultNextQuestionID>
    <pa-structures:QuestionType>
        <pa-structures:Date>
            <datatypes:IsDateTimeRequired>N</datatypes:IsDateTimeRequired>
        </pa-structures:Date>
    </pa-structures:QuestionType>
</pa-structures:Question>
<pa-structures:Question>
    <pa-structures:QuestionID>Q4</pa-structures:QuestionID>
    <pa-structures:SequenceNumber>4</pa-structures:SequenceNumber>
    <pa-structures:QuestionText>Is the medication to be used for treatment of DVT?</pa-structures:QuestionText>
    <pa-structures:QuestionType>
        <pa-structures:Select>
            <pa-structures:SelectMultiple>N</pa-structures:SelectMultiple>
            <pa-structures:Choice>
                <pa-structures:ChoiceID>Q4C1</pa-structures:ChoiceID>
                <pa-structures:SequenceNumber>1</pa-structures:SequenceNumber>
                <pa-structures:ChoiceText>Yes</pa-structures:ChoiceText>
                <pa-structures:AdditionalFreeTextIndicator>NA</pa-structures:AdditionalFreeTextIndicator>
                <pa-structures:NextQuestionID>Q5</pa-structures:NextQuestionID>
            </pa-structures:Choice>
            <pa-structures:Choice>
                <pa-structures:ChoiceID>Q4C2</pa-structures:ChoiceID>
                <pa-structures:SequenceNumber>2</pa-structures:SequenceNumber>
                <pa-structures:ChoiceText>No</pa-structures:ChoiceText>
                <pa-structures:AdditionalFreeTextIndicator>NA</pa-structures:AdditionalFreeTextIndicator>
                <pa-structures:NextQuestionID>Q6</pa-structures:NextQuestionID>
            </pa-structures:Choice>
        </pa-structures:Select>
    </pa-structures:QuestionType>
</pa-structures:Question>
<pa-structures:Question>
    <pa-structures:QuestionID>Q5</pa-structures:QuestionID>
    <pa-structures:SequenceNumber>5</pa-structures:SequenceNumber>
    <pa-structures:QuestionText>What is the patient's weight (in pounds)?</pa-structures:QuestionText>
    <pa-structures:DefaultNextQuestionID>Q6</pa-structures:DefaultNextQuestionID>
    <pa-structures:QuestionType>
        <pa-structures:Numeric>
            <pa-structures:IsNumeric>Y</pa-structures:IsNumeric>
        </pa-structures:Numeric>
    </pa-structures:QuestionType>
</pa-structures:Question>
<pa-structures:Question>
    <pa-structures:QuestionID>Q6</pa-structures:QuestionID>
    <pa-structures:SequenceNumber>6</pa-structures:SequenceNumber>
    <pa-structures:QuestionText>Describe result of Warfarin use (include INR and date drawn) or reason why patient can't use Warfarin.
    </pa-structures:QuestionText>
    <pa-structures:DefaultNextQuestionID>END</pa-structures:DefaultNextQuestionID>
    <pa-structures:QuestionType>

如果我将访问QuestionType标记,那么我将获得Q6信息。我无法访问q1到q5的信息。 我正在通过以下代码访问问题标签:

response_data.get('{http://www.ncpdp.org/schema/transport}Body', {})\
    .get('{http://www.ncpdp.org/schema/transport}PAInitiationResponse', {})\
    .get('{http://www.ncpdp.org/schema/script}Response', {})\
    .get('{http://www.ncpdp.org/schema/pa-structures}ResponseStatus', {}).data.get('{http://www.ncpdp.org/schema/pa-structures}Open', None)

我正在使用drf的xmlParser类。

0 个答案:

没有答案