获取xml字符串的所有标记

时间:2017-09-02 02:17:58

标签: javascript html xml

<listing>
<address>1440 Church Street NW #307</address>
<id>1884</id>
<mlsid>DC123456</mlsid>
<status>Sold</status>
<type>Condominium</type>
<offer>For Sale</offer>
<price>925000</price>
<beds>2</beds>
<baths>2</baths>
<parking>Off-Street</parking>
<description>
Lorem ipsum dolor sit amet, unum accusamus laboramus id sea, vix te consul cetero. Vis in amet appetere, mel an appetere scripserit sadipscing, everti dignissim delicatissimi ius ex. Has dictas suscipit in. Feugiat vocibus referrentur no mel, in scripserit sadipscing per, at usu facilisi interesset. Ius ut sapientem neglegentur, no enim essent aliquip cum, ad labitur senserit maiestatis est. Epicurei torquatos vim et, saepe pertinax at pri. Est et sale molestiae scriptorem, per ea cibo scriptorem.
</description>
<ibrochure></ibrochure>
<hoodtour>
</hoodtour>
</listing>

我尝试了什么

getAlltags(string xml){
  var everytag = xml.split('<');
  everytag.forEach(function(tagstring){
     var name = tagstring.split('>')[0];
     console.log(name);
   })
}

它正在获取标签列表,但我认为这不是好方法。 是否有搜索所有标签的在线代码? 我想从xml上面得到的结果是<listing>, <address>, <id>, <mlsid> ...

1 个答案:

答案 0 :(得分:4)

如果将XML字符串解析为实际的XML文档,然后只使用遍历方法和数组,那将会容易得多

&#13;
&#13;
var xmlString = "<listing><address>1440 Church Street NW 307</address><id>1884</id><mlsid>DC123456</mlsid><status>Sold</status><type>Condominium</type><offer>For Sale</offer><price>925000</price><beds>2</beds><baths>2</baths><parking>Off-Street</parking><description>Lorem ipsum dolor sit amet, unum accusamus laboramus id sea, vix te consul cetero. Vis in amet appetere, mel an appetere scripserit sadipscing, everti dignissim delicatissimi ius ex. Has dictas suscipit in. Feugiat vocibus referrentur no mel, in scripserit sadipscing per, at usu facilisi interesset. Ius ut sapientem neglegentur, no enim essent aliquip cum, ad labitur senserit maiestatis est. Epicurei torquatos vim et, saepe pertinax at pri. Est et sale molestiae scriptorem, per ea cibo scriptorem.</description><ibrochure></ibrochure><hoodtour>https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F97603062%3Ftitle%3D0%26byline%3D0%26portrait%3D0</hoodtour></listing>";

var parser = new DOMParser();
var doc    = parser.parseFromString(xmlString, "application/xml");
var tags   = [];

doc.querySelectorAll('*').forEach(el => {
    if ( tags.indexOf(el.tagName) === -1 ) tags.push(el.tagName);
});

console.log(tags);
&#13;
&#13;
&#13;

如果更多ES2015没问题,可以使用Set for unique values

&#13;
&#13;
var xml = "<listing><address>1440 Church Street NW 307</address><id>1884</id><mlsid>DC123456</mlsid><status>Sold</status><type>Condominium</type><offer>For Sale</offer><price>925000</price><beds>2</beds><baths>2</baths><parking>Off-Street</parking><description>Lorem ipsum dolor sit amet, unum accusamus laboramus id sea, vix te consul cetero. Vis in amet appetere, mel an appetere scripserit sadipscing, everti dignissim delicatissimi ius ex. Has dictas suscipit in. Feugiat vocibus referrentur no mel, in scripserit sadipscing per, at usu facilisi interesset. Ius ut sapientem neglegentur, no enim essent aliquip cum, ad labitur senserit maiestatis est. Epicurei torquatos vim et, saepe pertinax at pri. Est et sale molestiae scriptorem, per ea cibo scriptorem.</description><ibrochure></ibrochure><hoodtour>https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F97603062%3Ftitle%3D0%26byline%3D0%26portrait%3D0</hoodtour></listing>";

var parser = new DOMParser();
var doc    = parser.parseFromString(xml, "application/xml");
var tags  =  new Set([].map.call(doc.querySelectorAll('*'), z => z.tagName));

console.log(Array.from(tags));
&#13;
&#13;
&#13;