Skip to content

How to iterate over all nodes? #63

@Arlen22

Description

@Arlen22

I'm trying to use this code to iterate over child elements, but it doesn't work. I'm expecting it to log child\ntext 9 times but it logs undefined\n\n 3 times. If I log the child variable, it logs one XmlText instance 3 times.

const xml2 = XmlDocument.fromString(`
    <root>
      <parent>
        <parent2>
          <child>text</child>
          <child>text</child>
          <child>text</child>
        </parent2>
        <parent2>
          <child>text</child>
          <child>text</child>
          <child>text</child>
        </parent2>
        <parent2>
          <child>text</child>
          <child>text</child>
          <child>text</child>
        </parent2>
      </parent>
    </root>
    `);
  xml2.find("//parent").forEach(e => {
    e.find("parent2").forEach(e => {
      let child = e.firstChild;
      while (child) {
        console.log(child.name);
        console.log(child.content);
        child = child.nextSibling;
      }
    });
  });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions