Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6094635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:42:08+00:00 2026-05-23T12:42:08+00:00

I got confused when I tried to parse following xml using libxmljs: <?xml version=1.0

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:42:09+00:00Added an answer on May 23, 2026 at 12:42 pm

    Raynos is correct. The 5 nodes you are seeing are . Try this version of your xml.

    <?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>
    

    And this returns nothing because the first child is a text node with no children.

    xmlDoc.root().childNodes()[0].get("Title");
    

    Most xml libs paper over the details and do “the sensible thing” like return only element nodes. Libxmljs is a loose wrapper around the libxml2 library. But libxml2 doesn’t do that. So it helps to understand the libxml2 DOM model. In the XML standard for parsing into a DOM, text nodes are valid children just like elements. There are also entities, CDATA, etc. You have to deal with these explicitly. Try checking node.type() to see what type of node you’re dealing with. It’s easy to filter to only the elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code and got myself confused: I have a query that
I have tried searching for it online, but I got confused. I didn't get
I've got confused . We hear a lot about Provider in ASP.NET . Membership-Provider
today I got confused when doing a couple of <%=Html.LabelFor(m=>m.MyProperty)%> in ASP.NET MVC 2
I was actually going through the question 1636644 .. and got confused with this
I'm new in Reporting services and got little confused. in the screen shot you
I'm quite confused by something. I've got 2 select lists, and if you choose
I'm pretty confused. I have a point: x= -12669114.702301 y= 5561132.6760608 That I got
Got a class that serializes into xml with XMLEncoder nicely with all the variables
I've got a file INPUT that has the following contents: 123\n 456\n 789 I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.