Javascript 如何在jquery中替换xml节点值

Javascript 如何在jquery中替换xml节点值,javascript,jquery,Javascript,Jquery,这是我的代码,我正在使用firefox的firebug(console.info) 我的Xml <list> <com.abc.db.ConfigInfo> <cfgId>83</cfgId> <cfgName>test</cfgName> <cfgDesc>test</cfgDesc> <cfgType>test</cfgType> <fileName>csmcl

这是我的代码,我正在使用firefox的firebug(console.info)

我的Xml

<list>
<com.abc.db.ConfigInfo>
<cfgId>83</cfgId>
<cfgName>test</cfgName>
<cfgDesc>test</cfgDesc>
<cfgType>test</cfgType>
<fileName>csmclientbenz.xml</fileName>
<absolutePath>../webapps/csm/files//1-105101/csmclientbenz.xml</absolutePath>
<emailAddress>rmargasa@abc.com</emailAddress>
<projectId>1-105101</projectId>
<hostname>benz</hostname>
<createDate>2011-06-15 15:29:55.0 IST</createDate>
<updateDate>2011-06-15 15:29:55.0 IST</updateDate>
<state>1</state>
<productId>1</productId>
</com.abc.db.ConfigInfo>
<com.abc.db.ConfigInfo>
<cfgId>102</cfgId>
<cfgName>cfgname1</cfgName>
<cfgDesc>test</cfgDesc>
<cfgType>test</cfgType>
<fileName>csmclientestilo.xml</fileName>
<absolutePath>../webapps/csm/files//1-105101/csmclientestilo.xml</absolutePath>
<emailAddress>rmargasa@abc.com</emailAddress>
<projectId>1-105101</projectId>
<hostname>estilo</hostname>
<createDate>2011-06-20 18:26:03.0 IST</createDate>
<updateDate>2011-06-20 18:26:03.0 IST</updateDate>
<state>1</state>
<productId>1</productId>
</com.abc.db.ConfigInfo>
</list>

83
测试
测试
测试
csmclientbenz.xml
../webapps/csm/files//1-105101/csmclientbenz.xml
rmargasa@abc.com
1-105101
奔驰
2011-06-15 15:29:55.0伊斯特
2011-06-15 15:29:55.0伊斯特
1.
1.
102
cfgname1
测试
测试
csmclientestilo.xml
../webapps/csm/files//1-105101/csmclientestilo.xml
rmargasa@abc.com
1-105101
埃斯蒂洛
2011-06-20 18:26:03.0伊斯特
2011-06-20 18:26:03.0伊斯特
1.
1.
我的jqGrid代码

var xmlDoc = $.parseXML(xml); 
         $('#configDiv').empty();
            $('<div width="100%">')
            .attr('id','configDetailsGrid')
            .html('<table id="list1" width="100%"></table>'+
                    '<div id="gridpager"></div>'+
                '</div>')       
            .appendTo('#configDiv');    

            var grid = jQuery("#list1");

            grid.jqGrid({

              datastr : xml,
              datatype: 'xmlstring',
              colNames:['cfgId','','Name', 'Host', 'Description','Product', 'Type', 'Last Updated Time','Last Updated By',''],
              colModel:[
                  {name:'cfgId',index:'cfgId', width:90, align:"right", hidden:true},
                  {name:'',index:'', width:15, align:"right",edittype:'checkbox',formatter: "checkbox",editoptions: { value:"True:False"},editable:true,formatoptions: {disabled : false}},
                  {name:'cfgName',index:'cfgName', width:90, align:"right"},
                  {name:'hostname',index:'hostname', width:90, align:"right"},
                  {name:'cfgDesc',index:'cfgDesc', width:90, align:"right"},
                  {name:'productId',index:'productId', width:60, align:"right"},
                  {name:'cfgType',index:'cfgType', width:60, align:"right"},
                  {name:'updateDate',index:'updateDate',sorttype:'Date', width:120, align:"right"},
                  {name:'emailAddress',index:'emailAddress', width:120, align:"right"},
                  {name:'absolutePath',index:'absolutePath', width:90, align:"right", hidden:true},
              ],
              pager : '#gridpager',
              rowNum:10,
              scrollOffset:0,
              height: 'auto',

              autowidth:true,
              viewrecords: true,
              gridview: true,
              xmlReader: {
                  root : "list",
                  row: "com\\.abc\\.db\\.ConfigInfo",
                  userdata: "userdata",
                  repeatitems: false
              },
              onSelectRow: function(id,status){
                  var rowData = jQuery(this).getRowData(id); 
                  configid = rowData['cfgId'];
                  configname=rowData['cfgName'];
                  configdesc=rowData['cfgDesc'];
                  configenv=rowData['cfgType'];

                  if(status==true)
                  {

                  }

                  rowChecked=1;
                  currentrow=id;
                  },
              onCellSelect: function(rowid, index, contents, event) {
                  if(index==2)
                  {

                        $(xmlDoc).find('list com\\.abc\\.db\\.ConfigInfo').each(function()
                        {
                            //alert($(this).find('cfgId').text()+" "+configid);
                            if($(this).find('cfgId').text()==configid)  
                            {
                                configname=$(this).find('cfgName').text(); 
                                configdesc=$(this).find('cfgDesc').text();
                                configenv=$(this).find('cfgType').text();
                                filename=$(this).find('fileName').text();
                                updatedate=$(this).find('updateDate').text();
                                absolutepath=$(this).find('absolutePath').text();
                                productname=productMap[$(this).find('productId').text()];
                            }
                        });

                  }
               }

            });
            grid.jqGrid('navGrid','#gridpager',{edit:false,add:false,del:false});
var xmlDoc=$.parseXML(xml);
$('#configDiv').empty();
$('')
.attr('id','configDetailsGrid'))
.html(“”+
''+
'')       
.appendTo(“#configDiv”);
var grid=jQuery(“列表1”);
grid.jqGrid({
datastr:xml,
数据类型:“xmlstring”,
colNames:['cfgId','','Name','Host','Description','Product','Type','Last Updated Time','Last Updated By','','',
colModel:[
{名称:'cfgId',索引:'cfgId',宽度:90,对齐:“右”,隐藏:true},
{name:'',index:'',width:15,align:'right',edittype:'checkbox',formatter:'checkbox',editoptions:{value:'True:False},editable:True,formattoptions:{disabled:False},
{名称:'cfgName',索引:'cfgName',宽度:90,对齐:“right”},
{名称:'hostname',索引:'hostname',宽度:90,对齐:“right”},
{名称:'cfgDesc',索引:'cfgDesc',宽度:90,对齐:“右”},
{名称:'productId',索引:'productId',宽度:60,对齐:“right”},
{名称:'cfgType',索引:'cfgType',宽度:60,对齐:“right”},
{名称:'updateDate',索引:'updateDate',排序类型:'Date',宽度:120,对齐:“right”},
{名称:'emailAddress',索引:'emailAddress',宽度:120,对齐:“右”},
{名称:'absolutePath',索引:'absolutePath',宽度:90,对齐:“右”,隐藏:true},
],
寻呼机:“#gridpager”,
rowNum:10,
滚动偏移量:0,
高度:“自动”,
自动宽度:正确,
viewrecords:是的,
gridview:没错,
xmlReader:{
根:“列表”,
行:“com\\.abc\\.db\\.ConfigInfo”,
用户数据:“用户数据”,
重复项:false
},
OnSetRow:功能(id、状态){
var rowData=jQuery(this).getRowData(id);
configid=rowData['cfgId'];
configname=rowData['cfgName'];
configdesc=rowData['cfgDesc'];
configenv=rowData['cfgType'];
如果(状态==真)
{
}
rowChecked=1;
currentrow=id;
},
onCellSelect:函数(rowid、索引、内容、事件){
如果(索引==2)
{
$(xmlDoc).find('list com\\.abc\\.db\\.ConfigInfo')。每个(函数()
{
//警报($(this.find('cfgId').text()+“”+configid);
if($(this).find('cfgId').text()==configid)
{
configname=$(this.find('cfgName').text();
configdesc=$(this.find('cfgDesc').text();
configenv=$(this.find('cfgType').text();
filename=$(this.find('filename').text();
updatedate=$(this.find('updatedate').text();
absolutepath=$(this.find('absolutepath').text();
productname=productMap[$(this).find('productId').text();
}
});
}
}
});
jqGrid('navGrid','#gridpager',{edit:false,add:false,del:false});
这就是我得到的输出我的问题:如何替换xml的节点值

用我的解决方案更新


我使用了
xml=xml.replace(/1/g,“+productMap['1'])
通过添加一个
/g
它替换了我所有的字符串

我假设
xml
是一个字符串。jQuery不更改字符串,它只更改通过解析它创建的DOM表示。每次将字符串传递给jQuery时,它都会重新解析它

您需要保留对jQuery创建的对象的引用:

var $xml = $(xml);
// now change it using $xml
如果要再次“序列化”数据,有两种可能:

  • 或者使用:
    xml=$xml.outerHTML()
  • 或者将整个XML附加到某个伪元素

    var $xml = $('<dummy />').append(xml);
    
    var$xml=$('').append(xml);
    
    并使用
    xml=$xml.html()稍后

    您还可以在完成处理后附加它,以便此附加元素不会阻止当前选择器

更新:


我刚刚看到jQuery也提供了1.5版之后的功能。这可能也会有帮助。

所以问题是您更改了文本,但不知何故,它似乎不是持久的?什么是
xml
?@Felix Kling:是的,我在这里发布了我的xmlit,但是接下来我在数据源中使用
xml
,这会受到影响,并且不会显示所有的网格内容,或者我可以简单地使用
xml=xml.replace(/1/g,“+productMap['1'])var $xml = $('<dummy />').append(xml);