js通过正则表达式解析xml 获取指定的内容
存在这样的xml:
<?xml version="1.0" encoding="UTF-8"?> <interface xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.chinatax.gov.cn/tirip/dataspec/interfaces.xsd" version="DZFP1.0"> <globalInfo> <appId>ZZSDZFP</appId> <interfaceCode>EI.DYCOMCS.U.EC.INTRA</interfaceCode> <userName>410102999999251</userName> <passWord>123456</passWord> <requestCode></requestCode> <requestTime>2019-11-08 10:30:36</requestTime> <responseCode></responseCode> <dataExchangeId>111ADBHB20191010100003651</dataExchangeId> <fjh>0-2</fjh> <jqbh></jqbh> </globalInfo> <returnStateInfo> <returnCode>0011</returnCode><returnMessage>6Kej5p6Q6L6T5YWl5oql5paH5pWw5o2u5Y+R55Sf5byC5bi477yM6K+35pu05q2j5ZCO6YeN5paw5Lyg5YWl</returnMessage> </returnStateInfo> <Data> <dataDescription> <zipCode>0</zipCode> <encryptCode>0</encryptCode> <codeType>0</codeType> </dataDescription><content>PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48UkVTUE9OU0VfQ09NTU9OX0RZQ09NQ1MgY2xhc3M9IlJFU1BPTlNFX0NPTU1PTl8gRFlDT01DUyI+PERZWFhTIC8+PC9SRVNQT05TRV9DT01NT05fRFlDT01D<</content>
现在我要获取里面的returnCode 和returnMessage里面的内容
var xmlstr=<?xml version="1.0" encoding="UTF-8"?><interface xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.chinatax.gov.cn/tirip/dataspec/interfaces.xsd" version="DZFP1.0"><globalInfo><appId>ZZSDZFP</appId><interfaceCode>EI.DYCOMCS.U.EC.INTRA</interfaceCode><userName>410102999999251</userName><passWord>123456</passWord><requestCode></requestCode><requestTime>2019-11-08 10:30:36</requestTime><responseCode></responseCode><dataExchangeId>111ADBHB20191010100003651</dataExchangeId><fjh>0-2</fjh><jqbh></jqbh></globalInfo><returnStateInfo><returnCode>0011</returnCode><returnMessage>6Kej5p6Q6L6T5YWl5oql5paH5pWw5o2u5Y+R55Sf5byC5bi477yM6K+35pu05q2j5ZCO6YeN5paw5Lyg5YWl</returnMessage></returnStateInfo><Data><dataDescription><zipCode>0</zipCode><encryptCode>0</encryptCode><codeType>0</codeType></dataDescription><content>PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48UkVTUE9OU0VfQ09NTU9OX0RZQ09NQ1MgY2xhc3M9IlJFU1BPTlNFX0NPTU1PTl8gRFlDT01DUyI+PERZWFhTIC8+PC9SRVNQT05TRV9DT01NT05fRFlDT01D"; var pattern=/<returnCode>(.*)</returnCode>/i var code=pattern.exec(xmlstr); console.log(code[1]); var pattern1=/<returnMessage>(.*)</returnMessage>/i var msg=pattern1.exec(xmlstr); console.log(msg[1]);
最后打印出来的结果如下:
希望对你有所帮助