0

我正在 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是我必须在单击按钮时每隔一段时间解析的标签。

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

4

1 回答 1

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();
于 2012-10-26T20:06:27.843 回答