0

我在解析XML文件时发现了一个问题

这是XML 文件的示例(作为字符串):

<ns:fetchXmlResponse xmlns:ns=\ " http: / ws.src.com \>
    <ns:return>
        <?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
        <TemplateData>
            <TemplatePrescriptionData>
                <id>24</id>
                <noOfRefills>1</noOfRefills>
                <templateId>67</templateId>
                <templatePrescriptionMaintainanceId>0
                </templatePrescriptionMaintainanceId>
                <prescriptionName>Norco</prescriptionName>
                <prescriptionStrength>5/325</prescriptionStrength>
                <prescriptionUnit>mg</prescriptionUnit>
                <prescriptionForm>Tablet(s)</prescriptionForm>
                <prescriptionSignature>1-2tabs po q4-6 hours, prn
                </prescriptionSignature>
                <prescriptionWarnings></prescriptionWarnings>
                <prescriptionComments></prescriptionComments>
                <prescriptionQuantity>15</prescriptionQuantity>
                <statusCode>true</statusCode>
                <createDate>2009-07-22T00:00:00+05:30</createDate>
                <updateDate>2009-07-22T00:00:00+05:30</updateDate>
            </TemplatePrescriptionData>
            <TemplatePrescriptionData>
                <id>25</id>
                <noOfRefills>2</noOfRefills>
                <templateId>67</templateId>
                <templatePrescriptionMaintainanceId>0
                </templatePrescriptionMaintainanceId>
                <prescriptionName>Motrin
                </prescriptionName>
                <prescriptionStrength>600</prescriptionStrength>
                <prescriptionUnit>mg</prescriptionUnit>
                <prescriptionForm>Tablet(s)</prescriptionForm>
                <prescriptionSignature>1tab po q6 hours,
                </prescriptionSignature>
                <prescriptionWarnings></prescriptionWarnings>
                <prescriptionComments></prescriptionComments>
                <prescriptionQuantity>28</prescriptionQuantity>
                <statusCode>true</statusCode>
                <createDate>2009-07-22T00:00:00+05:30</createDate>
                <updateDate>2009-07-22T00:00:00+05:30</updateDate>
            </TemplatePrescriptionData>
            <TemplatePrescriptionData>
                <id>26</id>
                <noOfRefills>2</noOfRefills>
                <templateId>67</templateId>
                <templatePrescriptionMaintainanceId>0
                </templatePrescriptionMaintainanceId>
                <prescriptionName>Chlorhexidine</prescriptionName>
                <prescriptionStrength>0.12</prescriptionStrength>
                <prescriptionUnit>%</prescriptionUnit>
                <prescriptionForm>Mouth
                </prescriptionForm>
                <prescriptionSignature>rinse mouth bid
                </prescriptionSignature>
                <prescriptionWarnings></prescriptionWarnings>
                <prescriptionComments></prescriptionComments>
                <prescriptionQuantity>1</prescriptionQuantity>
                <statusCode>true</statusCode>
                <createDate>2009-07-22T00:00:00+05:30</createDate>
                <updateDate>2009-07-22T00:00:00+05:30</updateDate>
            </TemplatePrescriptionData>
            <TemplatePrescriptionData>
                <id>28</id>
                <noOfRefills>1</noOfRefills>
                <templateId>76</templateId>
                <templatePrescriptionMaintainanceId>0
                </templatePrescriptionMaintainanceId>
                <prescriptionName>Norco</prescriptionName>
                <prescriptionStrength>5/325</prescriptionStrength>
                <prescriptionUnit>mg</prescriptionUnit>
                <prescriptionForm>Tablet(s)</prescriptionForm>
                <prescriptionSignature>1-2tabs po q4-6 hours, prn
                </prescriptionSignature>
                <prescriptionWarnings></prescriptionWarnings>
                <prescriptionComments></prescriptionComments>
                <prescriptionQuantity>15</prescriptionQuantity>
                <statusCode>true</statusCode>
                <createDate>2009-07-22T00:00:00+05:30</createDate>
                <updateDate>2009-07-22T00:00:00+05:30</updateDate>
            </TemplatePrescriptionData>
            <TemplatePrescriptionData>
                <id>30</id>
                <noOfRefills>2</noOfRefills>
                <templateId>76</templateId>
                <templatePrescriptionMaintainanceId>0
                </templatePrescriptionMaintainanceId>
                <prescriptionName>Motrin
                </prescriptionName>
                <prescriptionStrength>600</prescriptionStrength>
                <prescriptionUnit>mg</prescriptionUnit>
                <prescriptionForm>Tablet(s)</prescriptionForm>
                <prescriptionSignature>1tab po q6 hours,prn
                </prescriptionSignature>
                <prescriptionWarnings></prescriptionWarnings>
                <prescriptionComments></prescriptionComments>
                <prescriptionQuantity>28</prescriptionQuantity>
                <statusCode>true</statusCode>
                <createDate>2009-07-22T00:00:00+05:30</createDate>
                <updateDate>2009-07-22T00:00:00+05:30</updateDate>
            </TemplatePrescriptionData>
            <TemplatePrescriptionData>
                <id>31</id>
                <noOfRefills>2</noOfRefills>
                <templateId>76</templateId>
                <templatePrescriptionMaintainanceId>0
                </templatePrescriptionMaintainanceId>
                <prescriptionName>Chlorhexidine</prescriptionName>
                <prescriptionStrength>0.12</prescriptionStrength>
                <prescriptionUnit>%</prescriptionUnit>
                <prescriptionForm>MouthRinse
                </prescriptionForm>
                <prescriptionSignature>rinse mouth bid(withexpectoration)
                </prescriptionSignature>
                <prescriptionWarnings></prescriptionWarnings>
                <prescriptionComments></prescriptionComments>
                <prescriptionQuantity>1</prescriptionQuantity>
                <statusCode>true</statusCode>
                <createDate>2009-07-22T00:00:00+05:30</createDate>
                <updateDate>2009-07-22T00:00:00+05:30</updateDate>
            </TemplatePrescriptionData>
            <TemplatePrescriptionData>
                <id>32</id>
                <noOfRefills>1</noOfRefills>
                <templateId>79</templateId>
                <templatePrescriptionMaintainanceId>0
                </templatePrescriptionMaintainanceId>
                <prescriptionName>Norco</prescriptionName>
                <prescriptionStrength>5/325</prescriptionStrength>
                <prescriptionUnit>mg</prescriptionUnit>
                <prescriptionForm>Tablet(s)</prescriptionForm>
                <prescriptionSignature>1-2tabs po q4-6 hours, prn</prescriptionSignature>
                <prescriptionWarnings></prescriptionWarnings>
                <prescriptionComments></prescriptionComments>
                <prescriptionQuantity>15</prescriptionQuantity>
                <statusCode>true</statusCode>
                <createDate>2009-07-22T00:00:00+05:30</createDate>
                <updateDate>2009-07-22T00:00:00+05:30</updateDate>
            </TemplatePrescriptionData>
            <TemplatePrescriptionData>
                <id>33</id>
                <noOfRefills>2</noOfRefills>
                <templateId>79</templateId>
                <templatePrescriptionMaintainanceId>0
                </templatePrescriptionMaintainanceId>
                <prescriptionName>Motrin
                </prescriptionName>
                <prescriptionStrength>600</prescriptionStrength>
                <prescriptionUnit>mg</prescriptionUnit>
                <prescriptionForm>Tablet(s)</prescriptionForm>
                <prescriptionSignature>1tab po q6 hours,prn</prescriptionSignature>
                <prescriptionWarnings></prescriptionWarnings>
                <prescriptionComments></prescriptionComments>
                <prescriptionQuantity>28</prescriptionQuantity>
                <statusCode>true</statusCode>
                <createDate>2009-07-22T00:00:00+05:30</createDate>
                <updateDate>2009-07-22T00:00:00+05:30</updateDate>
            </TemplatePrescriptionData>
        </TemplateData>
    </ns:return>
</ns:fetchXmlResponse>

代码:

var xmlDoc=loadXMLString(Above XML file);

//Then i'm fetching "ns:return" node(Which is also an XML file)
var returnNode = String(xmlDoc.getElementsByTagName("ns:return")[0].childNodes[0].nodeValue);

loadXMLString函数

function loadXMLString(txt)
{
if (window.DOMParser)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");
  }
else // Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.loadXML(txt);
  }

现在的问题是returnNode变量只有4048字符,尽管整个 xmlfile

样本:

<?xml version=\"1.0\" encoding=\"UTF-8\"    standalone=\"yes\"?>
<TemplateData>
    <TemplatePrescriptionData>
        <id>24</id>
        <noOfRefills>1</noOfRefills>
        <templateId>67</templateId>
        <templatePrescriptionMaintainanceId>0</templatePrescriptionMaintainanceId>
        <prescriptionName>Norco</prescriptionName>
        <prescriptionStrength>5/325</prescriptionStrength>
        <prescriptionUnit>mg</prescriptionUnit>
        <prescriptionForm>Tablet(s)</prescriptionForm>
        <prescriptionSignature>1-2 tabs po q4-6 hours, prn
        </prescriptionSignature>
        <prescriptionWarnings></prescriptionWarnings>
        <prescriptionComments></prescriptionComments>
        <prescriptionQuantity>15</prescriptionQuantity>
        <statusCode>true</statusCode>
        <createDate>2009-07-22T00:00:00+05:30</createDate>
        <updateDate>2009-07-22T00:00:00+05:30</updateDate>
    </TemplatePrescriptionData>
    <TemplatePrescriptionData>
        <id>25</id>
        <noOfRefills>2</noOfRefills>
        <templateId>67</templateId>
        <templatePrescriptionMaintainanceId>0
        </templatePrescriptionMaintainanceId>
        <prescriptionName>Motrin </prescriptionName>
        <prescriptionStrength>600</prescriptionStrength>
        <prescriptionUnit>mg</prescriptionUnit>
        <prescriptionForm>Tablet(s)</prescriptionForm>
        <prescriptionSignature>1 tab po q6 hours, prn</prescriptionSignature>
        <prescriptionWarnings></prescriptionWarnings>
        <prescriptionComments></prescriptionComments>
        <prescriptionQuantity>28</prescriptionQuantity>
        <statusCode>true</statusCode>
        <createDate>2009-07-22T00:00:00+05:30</createDate>
        <updateDate>2009-07-22T00:00:00+05:30</updateDate>
    </TemplatePrescriptionData>
    <TemplatePrescriptionData>
        <id>26</id>
        <noOfRefills>2</noOfRefills>
        <templateId>67</templateId>
        <templatePrescriptionMaintainanceId>0
        </templatePrescriptionMaintainanceId>
        <prescriptionName>Chlorhexidine</prescriptionName>
        <prescriptionStrength>0.12</prescriptionStrength>
        <prescriptionUnit>%</prescriptionUnit>
        <prescriptionForm>Mouth Rinse</prescriptionForm>
        <prescriptionSignature>rinse mouth bid (with expectoration)
        </prescriptionSignature>
        <prescriptionWarnings></prescriptionWarnings>
        <prescriptionComments></prescriptionComments>
        <prescriptionQuantity>1</prescriptionQuantity>
        <statusCode>true</statusCode>
        <createDate>2009-07-22T00:00:00+05:30</createDate>
        <updateDate>2009-07-22T00:00:00+05:30</updateDate>
    </TemplatePrescriptionData>
    <TemplatePrescriptionData>
        <id>28</id>
        <noOfRefills>1</noOfRefills>
        <templateId>76</templateId>
        <templatePrescriptionMaintainanceId>0
        </templatePrescriptionMaintainanceId>
        <prescriptionName>Norco</prescriptionName>
        <prescriptionStrength>5/325</prescriptionStrength>
        <prescriptionUnit>mg</prescriptionUnit>
        <prescriptionForm>Tablet(s)</prescriptionForm>
        <prescriptionSignature>1-2 tabs po q4-6 hours, prn
        </prescriptionSignature>
        <prescriptionWarnings></prescriptionWarnings>
        <prescriptionComments></prescriptionComments>
        <prescriptionQuantity>15</prescriptionQuantity>
        <statusCode>true</statusCode>
        <createDate>2009-07-22T00:00:00+05:30</createDate>
        <updateDate>2009-07-22T00:00:00+05:30</updateDate>
    </TemplatePrescriptionData>
    <TemplatePrescriptionData>
        <id>30</id>
        <noOfRefills>2</noOfRefills>
        <templateId>76</templateId>
        <templatePrescriptionMaintainanceId>0
        </templatePrescriptionMaintainanceId>
        <prescriptionName>Motrin </prescriptionName>
        <prescriptionStrength>600</prescriptionStrength>
        <prescriptionUnit>mg</prescriptionUnit>
        <prescriptionForm>Tablet(s)</prescriptionForm>
        <prescriptionSignature>1 tab po q6 hours, prn</prescriptionSignature>
        <prescriptionWarnings></prescriptionWarnings>
        <prescriptionComments></prescriptionComments>
        <prescriptionQuantity>28</prescriptionQuantity>
        <statusCode>true</statusCode>
        <createDate>2009-07-22T00:00:00+05:30</createDate>
        <updateDate>2009-07-22T00:00:00+05:30</updateDate>
    </TemplatePrescriptionData>
    <TemplatePrescriptionData>
        <id>31</id>
        <noOfRefills>2</noOfRefills>
        <templateId>76</templateId>
        <templatePrescriptionMaintainanceId>0
        </templatePrescriptionMaintainanceId>
        <prescriptionName>Chlorhexidine</prescriptionName>
        <prescriptionStrength>0.12</prescriptionStrength>
        <prescriptionUnit>%</prescriptionUnit>
        <prescriptionForm>Mouth Rinse</prescriptionForm>
        <prescriptionSignature>rinse mouth bid (with expectoration)
        </pr

注意:我已经尝试过,jQuery但首先它会验证数据并引发Invalid XML异常。
我还验证了它"The processing instruction target matching "[xX][mM][lL]" is not allowed."在我的文件中显示的这个文件。
谁能帮我解决这个问题,或者你能建议我一些棘手的方法来处理这种类型的 XML 文件吗?

4

1 回答 1

1

xml 中的 XML 将是有问题的,除非它被包装在<![CDATA[ ... ]]>其中允许您将内部 XML 作为字符串获取,然后您可以对其进行解析......

var inner = xmlDoc.getElementsByTagName("return")[0].textContent;

或者在解析之前从内部 xml 中删除 XML Processsing 指令,如果你的外部 XML 也有匹配的 PI,你可能需要更聪明一点。

var d= ...your xml...
d = d.replace( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" , "" );
var xmlDoc = loadXMLString(d);

var templateData = xmlDoc.getElementsByTagName("TemplateData");
于 2013-10-17T09:05:48.693 回答