在第二次尝试时无法正确解析XML

时间:2012-10-19 10:56:42

标签: javascript android xml-parsing titanium appcelerator

我正在开发Appcelerator Titanium中的应用程序。我面临的问题是,当我第一次在单击按钮时解析特定的XML时,它工作正常,但是如果我重新单击该按钮,则应该再次解析XML,这不会发生。在日志中,我可以在重新构造的XML中看到更新的值,但是当我尝试解析它时,解析器中使用的变量保留它们的值。他们没有得到更新。如何更新解析器的变量值?

解析代码:

if(xml.getElementsByTagName("d:user_id").length>0)
{
         Ti.App.information.user_id = xml.getElementsByTagName("d:user_id").item(0).text;
         Ti.App.information.organization_id=xml.getElementsByTagName("d:organization_id").item(0).text;
         Ti.App.information.uname=xml.getElementsByTagName("d:name").item(0).text;
         Ti.App.information.Lpassword=xml.getElementsByTagName("d:password").item(0).text;
         Ti.App.information.Lemail_id=xml.getElementsByTagName("d:email_id").item(0).text;
         Ti.App.information.is_valid=xml.getElementsByTagName("d:is_valid").item(0).text;
}

这是我使用 Ti.App.information.is_valid 变量的函数:

function checkCredential()
{
    if(Ti.Utils.md5HexDigest(txtPasswordLogin.value)==Ti.App.information.Lpassword && txtEmailLogin.value == Ti.App.information.Lemail_id)
    {
        activityIndicator.hide();
        //alert("welcome");
        //Login.close();
        if(Ti.App.information.is_valid == 'true')
        {
            //Ti.API.info("..........Login Success........Getting Approved Forms..........");
            Ti.App.information.is_valid='';
             //alert(Ti.App.is_valid);

            // Titanium.include('Login.js');
             // Login.open();
             Ti.App.information.is_valid = '';
            Titanium.include('Home.js');
            Home.open();

        }
        else
        {
            // var a = Titanium.UI.createAlertdialogScreen({title:'Access Denied!'});
            Ti.App.information.is_valid='';
             //alert(Ti.App.is_valid);
            myAlert.title = 'Access Denied!';
            myAlert.setMessage('You are not authorized. Please contact Administrator.');
            myAlert.addEventListener('click', function(e){
                // Titanium.include('Login.js');
            // Login.open();
              Login.close();
      });



        myAlert.show();     
    }       
}
else
{ 
    Ti.App.information.is_valid='';
     //alert(Ti.App.is_valid);
    activityIndicator.hide();
    myAlert.title = 'Invalid Login!';
    myAlert.setMessage('The email address or password you entered is incorrect.');

    myAlert.show();
    // Titanium.include('Login.js');
    // Login.open();
    // alert("Please enter a correct password.");   
}

}

变量在 app.js 文件中声明:

Ti.App.information={base_url:'',uid:'',password:'',EmailId:'',pass:'',user_id:'',organization_id:'',uname:'',Lpassword:'',Lemail_id:'',is_valid:''};
Ti.App.authentication=false;
Ti.App.formid;
Ti.App.guid;
Ti.App.mydata={_guid:'',status:true,networkStatus:true};
Ti.App.formurl={burl:''};
Ti.App.glocation={longitude:'',latitude:'',gloc:''};
Ti.App.gloc;

这是一个要解析的示例XML文件:

<feed xml:base="http://webdevhost.servehttp.com/iPhoneDataEntryService/iPhoneDataEntryDataService.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
   <title type="text">users</title>
   <id>http://webdevhost.servehttp.com/iPhoneDataEntryService/iPhoneDataEntryDataService.svc/users</id>
   <updated>2012-10-26T04:05:48Z</updated>
   <link rel="self" title="users" href="users" />
   <entry>
     <id>http://webdevhost.servehttp.com/iPhoneDataEntryService/iPhoneDataEntryDataService.svc/users(34L)</id>
     <title type="text"></title>
     <updated>2012-10-26T04:05:48Z</updated>
     <author>
       <name />
     </author>
     <link rel="edit" title="user" href="users(34L)" />
     <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/user_type" type="application/atom+xml;type=entry" title="user_type" href="users(34L)/user_type" />
     <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/organization" type="application/atom+xml;type=entry" title="organization" href="users(34L)/organization" />
     <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/form_data" type="application/atom+xml;type=feed" title="form_data" href="users(34L)/form_data" />
     <category term="iPhoneDataEntryModel.user" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
     <content type="application/xml">
       <m:properties>
         <d:user_id m:type="Edm.Int64">34</d:user_id>
         <d:organization_id m:type="Edm.Int64">1</d:organization_id>
         <d:name>test</d:name>
         <d:password>16690502e26791e97c6c4743d2d4cb9a</d:password>
         <d:email_id>ad@ad.com</d:email_id>
         <d:user_type_id m:type="Edm.Int32">3</d:user_type_id>
         <d:is_valid m:type="Edm.Boolean">true</d:is_valid>
         <d:registration_date m:type="Edm.DateTime">2012-10-18T10:33:13</d:registration_date>
       </m:properties>
     </content>
   </entry>
 </feed>

d:is_valid 是我必须在点击按钮时间隔解析的标记。

问题是即使在重新调用解析器之后,此变量仍保留其值。

1 个答案:

答案 0 :(得分:0)

可以改变对象值,这样做。我删除了一些没有被使用的对象,所以如果你想重新添加它们,请记住这一点。我将这个代码复制到一个新项目中,只是测试它并使用它。

Ti.App.information = {
    user_id:'existing username val',
    organization_id:'existing org val',
    uname:'existing uname val',
    Lpassword:'existing Lpassword val',
    Lemail_id:'existing Lemail_id val',
    is_valid:'existing is_valid val'
};

function parseXML(xml)
{
    if (xml.getElementsByTagName("d:user_id").length > 0)
    {
        for (key in Ti.App.information)
        {
            //outputs the key => value pairs for some existing data
            Ti.API.info(key + ': ' + Ti.App.information[key]); 
        }
        Ti.API.warn('------------------------');
        Ti.App.information = {
            user_id:            xml.getElementsByTagName("d:user_id").item(0).text,
            organization_id:    xml.getElementsByTagName("d:organization_id").item(0).text,
            uname:              xml.getElementsByTagName("d:name").item(0).text,
            Lpassword:          xml.getElementsByTagName("d:password").item(0).text,
            Lemail_id:          xml.getElementsByTagName("d:email_id").item(0).text,
            is_valid:           xml.getElementsByTagName("d:is_valid").item(0).text
        }

        for (key in Ti.App.information)
        {
            //outputs the key => value pairs for the new data you set above
            Ti.API.info(key + ': ' + Ti.App.information[key]); 
        }
    }
}

function loadXML()
{
    var xhr = Ti.Network.createHTTPClient({
        onload: function(event)
        {
            Ti.API.info('loaded');
            parseXML(this.responseXML);
        },
        onerror: function(event)
        {
            alert('Error. Code: ' + this.status);
        },
        timeout: 5000
    });
    xhr.open('GET','http://ronnieswietek.com/feed.xml');
    xhr.send();
}
loadXML();