0

我正在尝试以这种方式从 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>
4

1 回答 1

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/

于 2012-07-08T13:23:15.667 回答