忽略XML文件中的空nodeValue

时间:2016-05-24 08:39:54

标签: javascript xml

我在跳过空的nodeValues时遇到了问题。下面我通过从XML文件中读取不同的标签来创建对象。问题是,当尝试读取“Addresse5”的nodeValue时,我收到错误,说明nodevalue未定义。

    var getCustomerAddress1 = xmlDoc.getElementsByTagName('address1');
    var getCustomerAddress2 = xmlDoc.getElementsByTagName('address2');
    var getCustomerAddress3 = xmlDoc.getElementsByTagName('address3');
    var getCustomerAddress4 = xmlDoc.getElementsByTagName('address4');
    var getCustomerAddress5 = xmlDoc.getElementsByTagName('address5');
    var getCustomerNumber = xmlDoc.getElementsByTagName('customerNumber');


var txt2 = {};
    for (var i = 0; i < len; i++) {

        txt2 = {

            Addresse1: getCustomerAddress1[i].childNodes[0].nodeValue,
            Addresse2: getCustomerAddress2[i].childNodes[0].nodeValue,
            Addresse3: getCustomerAddress3[i].childNodes[0].nodeValue,
            Addresse4: getCustomerAddress4[i].childNodes[0].nodeValue,
            Addresse5: getCustomerAddress5[i].childNodes[0].nodeValue,

        };
    }

我想要的是跳过阅读或用一些文本替换空的nodeValue,但我不知道如何实现这一点。

编辑:

这是我的xml文件的样子

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<collection>
    <DeliveryAddress>
        <address1>sdasdsadsa</address1>
        <address2>1asdasdas1</address2>
        <address3>sdfsdf</address3>
        <address4>Daasdasd</address4>
        <address5></address5>
        <customerNumber>5825252</customerNumber>
    </DeliveryAddress>
</collection>

提前致谢

2 个答案:

答案 0 :(得分:1)

您可以创建一个过滤未定义值的函数,并用空字符串或任何您喜欢的值替换它们,并将值传递给它,只要您无法确定XML文档中是否存在允许的值。

$email = 'blazriku@gmail.com , henrikus.antony@gmail.com';
// $subject = array("send message", "Welcome");
// $message = array("send message", "Welcome");
$name = array('Admin','Admin');
$to=$email;
$welcome_msg_user_group = array('henrikus.antony@gmail.com');
$send_msg_user_group = array('blazriku@gmail.com');

$welcome_email = array('subject' => 'Welcome', 'msg_body' => 'blah blah');
$send_email = array('subject' => 'Send message', 'msg_body' => 'blah blah send msg body');

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

// More headers
$headers .= 'From: kebunbibit.id <noreply@yourwebsite.com>'."\r\n" . 'Reply-To: '.$name.' <'.$email.'>'."\r\n";
$headers .= 'Cc: admin@yourdomain.com' . "\r\n"; //untuk cc lebih dari satu tinggal kasih koma    
foreach($welcome_msg_user_group as $welcome_user) {
    @mail($welcome_user, $welcome_email['subject'], $welcome_email['msg_body'], $headers);
}
foreach($send_msg_user_group as $send_user) {
    @mail($send_user, $send_email['subject'], $send_email['msg_body'], $headers);
}

if(@mail)
{
    print "<script>window.alert('E-Mail Terkirim!')</script>";
    print "<script>window.location='home.php?page=surat_jalan'</script>";
}
else {
    print "<script>window.alert('E-Mail Gagal Terkirim!')</script>";
    print "<script>window.location='home.php?page=surat_jalan'</script>";
}

答案 1 :(得分:0)

您可以使用continue operator使用for in operator,如下所示:

for(var prop in txt2){
  if(prop.length < 1) continue //skips if length is less than 1
  //perform your task now...
}