1

I am new to Angular and I want to read a specific node from the XML file. example: let's say I have an XML file emp.xml

    `<employees>
    <employee>
    <id>1</id>
    <name>Iqbal</name>
    <address>xxxxx</address>
    <contact>xxxxxx88xx</contact>
    </employee>
    <employee>
    <id>2</id>
    <name>Anil</name>
    <address>xxxxx</address>
    <contact>xxxxxx88xx</contact>
    </employee>
    </employees>`

now I want to read this whole file. and then I want to extract specific node i.e name of the employee. how can I achieve that?

2 Answers 2

2

You should be able to do this in plain old JS:

const myXML = new DOMParser().parseFromString('<xml></xml>', 'text/xml');

If you wanted to get your first employee then, you can get it like this:

const firstEmployee = myXML.getElementsByTagName('employee')[0];
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

 let xml_content= `<employees>
    <employee>
    <id>1</id>
    <name>Iqbal</name>
    <address>xxxxx</address>
    <contact>xxxxxx88xx</contact>
    </employee>
    <employee>
    <id>2</id>
    <name>Anil</name>
    <address>xxxxx</address>
    <contact>xxxxxx88xx</contact>
    </employee>
    </employees>`;
    
    var parser = new DOMParser();
    let xmlDoc = parser.parseFromString(xml_content, 'text/xml'); 
    let firstEmploye = xmlDoc.getElementsByTagName('employee')[0];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.