我將XML存儲在變量'ad'和'addd'中。如何在Javascript中檢查XML元素值的存在
變量 '廣告':
<?xml version="1.0" encoding="UTF-8"?>
<name>
<data>
<Id>003</Id>
</data>
<data>
<Id>006</Id>
</data>
....
</name>
和可變 'ADDD':
<?xml version="1.0" encoding="UTF-8"?>
<name>
<data>
<Id>009</Id>
</data>
<data>
<Id>005</Id>
</data>
...
</name>
我已經寫了ttt
功能:
function ttt(ad,addd) {
var match = ad.match(/<Id\/>/);
var matcht = addd.match(/<Id\/>/);
if ((! match || match.length == 0) && (! matcht || matcht.length == 0)){
return "Below is the details of the Id of ad:\n\n" + ad.split("<Id>")[1].split("</Id>")[0]; + "\n\n And, Below is the details of the Id of addd:\n\n" +addd.split("<Id>")[1].split("</Id>")[0];
}
}
我需要If
Id
是不是從「廣告」變量空白,則作爲error
返回警報,Id
如下:
<?xml version="1.0" encoding="UTF-8"?>
<name>
<data>
<Id>003</Id>
</data>
<data>
<Id>006</Id>
</data>
</name>
而且,我需要If
Id
不是空白的「ADDD」,那麼變量error
返回警報,Id
如下:
<?xml version="1.0" encoding="UTF-8"?>
<name>
<data>
<Id>009</Id>
</data>
<data>
<Id>005</Id>
</data>
</name>
不要使用正則表達式解析XML。使用DOM解析器。 – Bergi
@Bergi我只有這樣做。但這不是按照預期產出提供的 – NL1