无法使用JQuery循环XML解析

时间:2012-07-08 13:08:40

标签: php jquery xml parsing post

我试图以这样的方式从XML中提取文本:
[文本] [文本中的选择] [文本继续] [另一个文本] [另一个选择] [另一个文本继续]

我设法部分地这样做了,所以它提取了第一段和第一段的选择,但它没有提取第二段的选择,不明白为什么。 (在示例中,第一段和第二段的选项相同)。

此外,我使用AJAX将所选内容的索引发布到php和mysql,但我不知道如何分离选择。 (如:首选 - > 1;第二选择 - > 3;等等)

我的代码是:

function parseXml(xml)
{
var myArray = [];
var i = 0;

//Megkeressük az összes paragrafust
$(xml).find("Paragraph").each(function()
{   
    $(this).find("text").each(function(){
    {

        $("#wrapper").append('<div id="text">' + $(this).text()) + '</div><br />';
    $("#text").append('<div id="choice"></div>');   

    }       
});         
});

//Each paragraph
$(xml).find("Paragraph").each(function()
{  //Megkeressük az összes választékot  
$(this).find("choice").each(function()
{
    myArray.push($(this).text());

 });
});

 $("#choice").append('&nbsp;' + myArray[i] + '&nbsp;');
    sendValue(i); 


$("#choice").click(function() {

    if(i + 1 >=  myArray.length)
    {
            i=0;
            $("#choice").html('&nbsp;' + myArray[i] + '&nbsp;');
            sendValue(i);   
    }       

    else
    {       i++;
            $("#choice").html('&nbsp;' + myArray[i] + '&nbsp;');        
            sendValue(i); 
    }

});

function sendValue(str){

// post(file, data, callback, type); (only "file" is required)
$.post(

"ajax.php", //Ajax file

{ sendValue: str },  // create an object will all values

//function that is called when server returns a value.
function(data){
    $('#i').text(data.returnValue);
}, 

//How you want the data formated when it is returned from the server.
"json"
);

}

}

PHP:

<?php 

//Get Post Variables. The name is the same as 
//what was in the object that was sent in the jQuery
if (isset($_POST['sendValue'])){
$value = $_POST['sendValue'];   
}else{
$value = "";
}

//Because we want to use json, we have to place things in an array and encode it for json.
//This will give us a nice javascript object on the front side.
 echo json_encode(array("returnValue"=>"This is returned from PHP : ".$value));  

?>

XML:

<?xml version="1.0" encoding="utf-8" ?>
<Page1>
<Paragraph>
<text>This is the first text in the</text><choice>paragraph</choice><choice>book</choice><choice>mádörfákör</choice><choice>asd</choice><text>after which the text continues.</text>
</Paragraph>
<Paragraph>
<text>This is the SECOND text in the</text><choice>paragraph</choice><choice>book</choice><choice>mádörfákör</choice><choice>asd</choice><text>after which the second text continues.</text>
</Paragraph>
</Page1>

1 个答案:

答案 0 :(得分:0)

这样的事情怎么样?

$(xml).find("Paragraph").each(function() {
    var paragraph = $('<div class="paragraph"></div>');
    paragraph.appendTo('#wrapper');

    var choiceCount = 0;
    $(this).find("text,choice").each(function() {
        if ($(this).get(0).tagName == 'TEXT') {
            $('<span class="paragraph-text"></span>').appendTo(paragraph).text($(this).text());
        } else {
            var choice = $('<span class="paragraph-choice"></span>').appendTo(paragraph).text($(this).text());
            (function(choiceCount) {
                choice.click(function() {
                    sendValue(choiceCount);
                });
            })(choiceCount);
            choiceCount++;
        }
    });
});

保存(并且正常工作):http://jsfiddle.net/XSuBU/1/