使用jQuery获取部分xml

使用jQuery获取部分xml,jquery,xml,Jquery,Xml,我有一个xml,看起来像这样 <page id="1"> <fields> <field typeid="4" id="1" order="1" qnumber="1"> <property> <label>question1</label> <options&

我有一个xml,看起来像这样

    <page id="1">
        <fields>
            <field typeid="4" id="1" order="1" qnumber="1">
                <property>
                    <label>question1</label>
                    <options>
                        <option id="18" order="2">option 1</option>
                        <option id="19" order="3">option 2</option>
                        <option id="20" order="4">option 3</option>
                    </options>
                    <maxOptions></maxOptions>
                    <isrequired>false</isrequired>
                </property>
            </field>
            <field typeid="3" id="5" order="5" qnumber="2">
                <property>
                    <label>question 5</label>
                    <options>
                        <option id="21" order="6">option 1</option>
                        <option id="22" order="7">option 2</option>
                        <option id="23" order="8">option 3</option>
                    </options>
                    <maxOptions></maxOptions>
                    <isrequired>false</isrequired>
                </property>
            </field>
        </fields>
    </page>
通过输入值,我希望能够仅显示用户选择的xml(用于保存)

所以我用了:

$(xmlDoc).find('field').each(function ()
{        

    $("#output").val($(this).val());

});

但它不会显示任何内容,我尝试了.text(),但也无法正常工作,有人能给我指出一个正确的方向吗?谢谢

如果您在html正文中,通常可以使用
$(this).html()
(您尝试使用
$(this).val()
),但这在xml文档中不可用

发件人:

此方法在XML文档上不可用

因此,我知道的唯一选择是通过访问所有子项来重建xml数据。如果您已经知道了结构,这会使重建变得更容易。类似于以下内容:

var xmlDoc = $.parseXML(xmlText);
var output="";    
    $(xmlDoc).find( "field" ).each(function() {
        $(this).find( "property" ).each(function() {
            output+= '<property>';
            $(this).find( "label" ).each(function() {    
                output+= '<label>';
                output+= $(this).text();
                output+= '</label>\n';
            });
            //FILL IN options, maxOptions and isRequired here ..
            output+= '</property>\n';
        });
    });
alert(output);
var xmlDoc=$.parseXML(xmlText);
var输出=”;
$(xmlDoc).find(“字段”).each(函数(){
$(this.find(“property”).each(function(){
输出+='';
$(this.find(“label”).each(function(){
输出+='';
输出+=$(this.text();
输出+='\n';
});
//在此处填写选项、maxOptions和isRequired。。
输出+='\n';
});
});
警报(输出);
我只做了一部分。您必须自己完成选项、maxOptions和isRequired字段。请参阅访问属性的
attr()
方法:

您可以在这里看到我的示例:


HTH

为了节省时间,我编写了一个小函数,它将获取一个xml节点并将其输出到字符串中。我希望这将对将来需要它的人有所帮助:

function XMLtoString(xmlNode)
{
    var rstr = "";
    var node = $(xmlNode).get(0);

    rstr = "<" + node.nodeName;

    if (node.attributes.length > 0)
    {
        for (var i = 0; i < node.attributes.length; i++)
        {
            rstr += " " + node.attributes[i].name + "=\"" + node.attributes[i].value + "\"";
        }
    }

    rstr += ">";

    if ($(node).children().length > 0)
    {
        $(node).children().each(function ()
        {
            rstr += XMLtoString($(this));
        });
    }
    else //asume reached the end childnode, then only write the value (not attribute)
    {
        rstr += $(node).text();
    }

    return rstr += "</" + node.nodeName + ">";
}
函数XMLtoString(xmlNode)
{
var rstr=“”;
var node=$(xmlNode).get(0);
rstr=“”;
if($(节点).children().length>0)
{
$(节点).children().each(函数)()
{
rstr+=XMLtoString($(this));
});
}
else//asume到达end childnode,然后只写入值(而不是属性)
{
rstr+=$(节点).text();
}
返回rstr+=“”;
}

快乐编码!!!^^

谢谢你的评论。我想我最后的办法是走这条路,但这需要一些工作。假设有任何方法可以获得每个节点的名称吗?我想使用递归方法,只需遍历每个子节点、每个属性并写出它。你知道如何获得节点名称?例如,如果我使用$(this).children().each(函数{find node name and attribute and write out xml here});我假设没有$(this).xmlNodeName()?在其他页面上找到某个内容,使用this.nodeName(不带美元符号)将给出节点名称,我可以使用此$(xml).find('item')遍历每个属性。each(function(){$.each(this.attributes,function(i,attrib){var name=attrib.name;var value=attrib.value;//…});});假设我应该提出一个函数,其中用户只需传入xml,该函数就会吐出字符串(待续…)
function XMLtoString(xmlNode)
{
    var rstr = "";
    var node = $(xmlNode).get(0);

    rstr = "<" + node.nodeName;

    if (node.attributes.length > 0)
    {
        for (var i = 0; i < node.attributes.length; i++)
        {
            rstr += " " + node.attributes[i].name + "=\"" + node.attributes[i].value + "\"";
        }
    }

    rstr += ">";

    if ($(node).children().length > 0)
    {
        $(node).children().each(function ()
        {
            rstr += XMLtoString($(this));
        });
    }
    else //asume reached the end childnode, then only write the value (not attribute)
    {
        rstr += $(node).text();
    }

    return rstr += "</" + node.nodeName + ">";
}