0

iam trying to update a ajax inbox. I'm having problems with parsing xml to jquery. I'm allways getting the error, that this isnt an xml file. But its (think?) correct and have no code-mistakes in it.

XML Output:

<?xml version='1.0' ?>
<inboxdata>
 <convid>2</convid>
 <sender_id>1</sender_id>
 <reciever_id>2</reciever_id>
 <msg_count>4</msg_count>
 <last_txt_msg>Hello</last_txt_msg>
 <sender_read>1</sender_read>
 <sender_delete>0</sender_delete>
 <time>23.09.2013 - 19:38</time>
 <userpic>img/ava/1.png</userpic>
</inboxdata>
<inboxdata>
 <convid>4</convid>
 <sender_id>1</sender_id>
 <reciever_id>3</reciever_id>
 <msg_count>0</msg_count>
 <last_txt_msg>No msg</last_txt_msg>
 <sender_read>1</sender_read>
 <sender_delete>0</sender_delete>
 <time>23.09.2013 - 20:25</time>
 <userpic>img/ava/1.png</userpic>
</inboxdata>

PHP:

function show_inbox($mysqli)
{
    if(!isset($_SESSION)){
    sec_session_start();
    }
    $userid = $_SESSION['user_id'];
    $get_conversation = $mysqli->query("SELECT * FROM conversation WHERE sender_id =" . $userid);
    $xml_node = "";
    while ($row = mysqli_fetch_array($get_conversation)) {
            $xml_node     .= "<inboxdata>\n";
            $xml_node     .= "<convid>".$row['convid']."</convid>\n";
            $xml_node     .= "<sender_id>".$row['sender_id']."</sender_id>\n";
            $xml_node     .= "<reciever_id>".$row['reciever_id']."</reciever_id>\n";
            $xml_node     .= "<msg_count>".$row['msg_count']."</msg_count>\n";
            $xml_node     .= "<last_txt_msg>".$row['last_txt_msg']."</last_txt_msg>\n";
            $xml_node     .= "<sender_read>".$row['sender_read']."</sender_read>\n";
            $xml_node     .= "<sender_delete>".$row['sender_delete']."</sender_delete>\n";
            $xml_node     .= "<time>".$row['time']."</time>\n";
            $xml_node     .= "<userpic>img/ava/1.png</userpic>\n";
            $xml_node     .= "</inboxdata>\n";
    }
    $returnXML = "<?xml version='1.0' ?>\n".$xml_node;
    echo $returnXML;
}

java:

function show_inbox()
    {
        result = $.ajax({
        type: 'GET',
        async: false,   // WICHTIG! 
        url: 'functions_chat.php',
        data: ({
            a: "show_inbox"
        }),
       success: function(response){
           alert($(response).find('inboxdata').length);
          $(response).find('inboxdata').each(function(){

         var convid   = $(this).find('convid').text();
         $("ul#chatinbox").prepend("<li>"+convid+"</li");

         });
       }
    }).responseText;
    }

If the Alert from alert($(response).find('inboxdata').length); shows up, it allways counts 0. It should count 2?

If I use $.parseXML(response) I'll get the XML Error: Invalid XML... But why its invalid? It seems valid to me!

Wheres the problem in here? Thanks!

2
  • You have 2 rootnodes, there can be only 1 [/end Highlander] (create a containing element (<list>?) which holds both <inboxdata> nodes. Commented Sep 23, 2013 at 20:41
  • I think you mean "pass" and "passing", not "parse" and "parsing" - a subtle point, but worth getting right in a context like this where both could apply: To "parse" XML would be to read it, to "pass" it implies sending it somewhere, so they're kind of opposites. Commented Sep 23, 2013 at 20:44

1 Answer 1

1

Your XML is invalid because it does not contain a root element (I use the name 'root' in the sample below, but you can use any name):

<?xml version='1.0' ?>
<root>
  ... your actual content here
</root>

You can check your XML using the W3 Online Validator: http://validator.w3.org/check

Sign up to request clarification or add additional context in comments.

2 Comments

thats freakin hilarious! LOL! I thought <inboxdata></inboxdata> should be enough. Well, time to learn XML exactly! Thanks alot. Works!
@user2447291 The key is that there must be exactly one outermost element, so if you have a loop, there has to be something outside of the loop too. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.