I got confused when I tried to parse following xml using libxmljs:
<?xml version="1.0" encoding="UTF-8"?>
<ResultSet>
<Result>
<Title>Title 1</Title>
<Summary>Summary 1</Summary>
</Result>
<Result>
<Title>Title 2</Title>
<Summary>Summary 2</Summary>
</Result>
</ResultSet>
I tried following code:
var libxmljs = require("libxmljs");
var xmlDoc = libxmljs.parseXmlFile("sample.xml");
xmlDoc.root().childNodes().length; // 5
I thought length property of third line should be 2, because there are 2 Result nodes, which are children of root node.
By the way, I inspected each elements like:
xmlDoc.root().childNodes()[0].get("Title").text(); // TypeError: Cannot call method 'text' of undefined
xmlDoc.root().childNodes()[1].get("Title").text(); // Title 1
xmlDoc.root().childNodes()[2].get("Title").text(); // TypeError: Cannot call method 'text' of undefined
xmlDoc.root().childNodes()[3].get("Title").text(); // Title 2
xmlDoc.root().childNodes()[4].get("Title").text(); // TypeError: Cannot call method 'text' of undefined
Why there are some irrelevant child nodes? Thanks in advance!
(my libxmljs version is 0.4.2)