使用jQuery解析XML文件中的表

时间:2018-09-07 16:47:30

标签: jquery xml parsing

我必须解析一个具有这样一部分的xml文件:

<part>configuration</part>
<table>
  <thead>
    <tr>
      <th>Tester Name</th>
      <th>Campaign Name</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>john</td>
      <td>random</td>
    </tr>
  </tbody>
</table>

问题是我也必须解析另一个<table>,即与另一个<part>名称在同一文件中。 我进行了很多搜索,但是找不到函数或方法来检索例如测试人员姓名和广告系列名称。 我是jquery的初学者,这可能就是为什么我没有找到/理解如何解析这些数据的原因。 谢谢大家对我有帮助! 无论是链接到教程还是功能建议。

1 个答案:

答案 0 :(得分:0)

您可以使用jQuery.parseXML()将字符串解析为XML文档。

然后,您需要Next Adjacent Selector (“prev + next”)才能选择零件元素旁边的表格。为了选择带有指定文本的零件元素,您需要使用:contains() Selector

摘要:

System.AggregateException: 'One or more errors occurred. (Webpack dev middleware failed because of an error while loading 'aspnet-webpack'. Error was: Error: Cannot find module 'webpack'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\knockout1\node_modules\aspnet-webpack\WebpackDevMiddleware.js:4:15)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
Current directory is: C:\Users\knockout1
)'
var xml = `<start>
              <part>configuration</part>
               <table>
                  <thead>
                     <tr>
                        <th>Tester Name</th>
                        <th>Campaign Name</th>
                    </tr>
                 </thead>
                 <tbody>
                    <tr>
                      <td>john</td>
                      <td>random</td>
                    </tr>
                 </tbody>
              </table>
          </start>`;
          
          
var xmlDoc = $.parseXML( xml );  // parse xml string
var xml = $( xmlDoc );    // get the jQuery object
// find the table next to part tag containing.....
var tbl = $xml.find( "part:contains(configuration) + table" );

console.log(tbl.get(0).outerHTML);