使用 Javascript/E4X 计算 XML 片段中的节点数

发布于 2024-07-28 23:01:52 字数 461 浏览 1 评论 0原文

考虑这个问题:

使用 Javascript/E4X,在非浏览器使用场景(Javascript HL7 集成引擎)中,有一个变量保存可能有多个重复节点的 XML 片段。

<pets>       
 <pet type="dog">Barney</pet>
 <pet type="cat">Socks</pet>
</pets>

问题:Javascript/E4X中如何获取宠物节点的数量?

编辑:为了澄清,这个问题应该围绕E4X(ECMAScript for XML)。 对那些没有提供此信息的人表示歉意。 我应该研究一下& 事先发布了此信息。

Consider this problem:

Using Javascript/E4X, in a non-browser usage scenario (a Javascript HL7 integration engine), there is a variable holding an XML snippet that could have multiple repeating nodes.

<pets>       
 <pet type="dog">Barney</pet>
 <pet type="cat">Socks</pet>
</pets>

Question: How to get the count of the number of pet nodes in Javascript/E4X ?

EDIT: To clarify, this question should be around E4X (ECMAScript for XML). Apologies to those who answered without this information. I should have researched & posted this info beforehand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南风起 2024-08-04 23:01:52

使用 E4X XML 对象构建“宠物”节点的 XMLList。 然后您可以调用 XMLList 上的 length 方法。

//<pets>
//    <pet type="dog">Barney</pet>
//    <pet type="cat">Socks</pet>
//</pets>  

// initialized to some XML resembling your example
var petsXML = new XML("");  

// build XMLList
var petList = petsXML['pet'];  

// alternative syntax
// var petList = petsXML.pet;  

// how many pet nodes are under a given pets node?
var length = petList.length();

Use an E4X XML object to build an XMLList of your 'pet' nodes. You can then call the length method on the XMLList.

//<pets>
//    <pet type="dog">Barney</pet>
//    <pet type="cat">Socks</pet>
//</pets>  

// initialized to some XML resembling your example
var petsXML = new XML("");  

// build XMLList
var petList = petsXML['pet'];  

// alternative syntax
// var petList = petsXML.pet;  

// how many pet nodes are under a given pets node?
var length = petList.length();
短暂陪伴 2024-08-04 23:01:52

我想说...使用 jQuery!

  var count = 0;
  $(xmlDoc).find("pet").each(function() {
          count++;

          // do stuff with attributes here if necessary.
          // var pet = $(this);
          // var myPetType = pet.attr("type");
       };
  });

编辑:不能使用jquery...好吧,让我们用常规的javascript来做:(

      var pets= xmlDoc.getElementsByTagName("pet");
      for ( var i = 0; i < pets.length ; i++ )
      {
          count++;
         // var petObj = {
         //     pets[i].getAttribute("type")
         //  };
      }

I'd say... use jQuery!

  var count = 0;
  $(xmlDoc).find("pet").each(function() {
          count++;

          // do stuff with attributes here if necessary.
          // var pet = $(this);
          // var myPetType = pet.attr("type");
       };
  });

EDIT: Can't use jquery... ok, let's do it in regular javascript :(

      var pets= xmlDoc.getElementsByTagName("pet");
      for ( var i = 0; i < pets.length ; i++ )
      {
          count++;
         // var petObj = {
         //     pets[i].getAttribute("type")
         //  };
      }
姐不稀罕 2024-08-04 23:01:52

使用 DOM,您可以调用 getElementsByTagName()

alert( document.getElementsByTagName( "pet" ).length );

更多信息:

https://developer.mozilla.org/en/DOM:element.getElementsByTagName

您将需要以某种方式获取 XML 文档。 例如,XHR 以解析的 XML 形式返回文档。

var xhr = new XMLHttpRequest();  
xhr.open( 'GET', 'http://domain.com/pets.xml', true );  
xhr.onreadystatechange = function ( e ) {  
    if ( xhr.readyState == 4 && xhr.status == 200 )
        alert( xhr.responseXML.getElementsByTagName( "pet" ).length );
};
xhr.send( null );  

Using the DOM you can call getElementsByTagName().

alert( document.getElementsByTagName( "pet" ).length );

More information:

https://developer.mozilla.org/en/DOM:element.getElementsByTagName

You're going to need to obtain the XML document somehow. XHR, for example, returns documents as parsed XML.

var xhr = new XMLHttpRequest();  
xhr.open( 'GET', 'http://domain.com/pets.xml', true );  
xhr.onreadystatechange = function ( e ) {  
    if ( xhr.readyState == 4 && xhr.status == 200 )
        alert( xhr.responseXML.getElementsByTagName( "pet" ).length );
};
xhr.send( null );  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文