解析xml时出现问题

时间:2013-10-10 10:47:18

标签: javascript jquery xml xml-parsing ibm-mobilefirst

我在解析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 funciton

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

1 个答案:

答案 0 :(得分: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");