I Read xml is used to store and transfer data can you
explain it with sample code for storing and displaying data.



I Read xml is used to store and transfer data can you explain it with sample code for storing and ..

Answer / Sachin Verma

XML (Extensible Markup Language) is a markup language used for storing, transporting, and exchanging structured data. Here's an example of using XML for storing and displaying data:

Storing Data:

```xml
<employees>
<employee id='001'>
<name>John Doe</name>
<age>30</age>
<position>Software Developer</position>
</employee>
<!-- More employee records here -->
</employees>
```
Displaying Data:

```java
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class XMLProcessor {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("employees.xml");

NodeList employeeNodes = doc.getElementsByTagName("employee");
for (int i = 0; i < employeeNodes.getLength(); i++) {
Element employeeElement = (Element) employeeNodes.item(i);

System.out.println("Employee " + (i+1) + ":");
System.out.println("ID: " + employeeElement.getAttribute("id"));
System.out.println("Name: " + getTextContent(employeeElement, "name"));
System.out.println("Age: " + getTextContent(employeeElement, "age"));
System.out.println("Position: " + getTextContent(employeeElement, "position"));
}
}

private static String getTextContent(Element element, String tagName) {
NodeList nodeList = element.getElementsByTagName(tagName);
if (nodeList != null && nodeList.getLength() > 0) {
return nodeList.item(0).getTextContent();
}
return null;
}
}
```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More XML XSLT RSS Interview Questions

Can a root element type be explicitly declared in the dtd?

1 Answers  


How do I change a text file to xml?

1 Answers  


Using XSLT, how would you extract a specific attribute from an element in an XML document?

1 Answers  


What does it mean to subscribe to a blog?

1 Answers  


What is xml web service?

1 Answers  


How dtd is different from xml?

1 Answers  


Why was xml created?

1 Answers  


What is the relevance of elementformdefault attribute in the schema?

1 Answers  


What is an atom syndication feed?

1 Answers  


Does anyone still use rss?

1 Answers  


What is the difference between wsdl and xsd?

1 Answers  


Briefly explain the xml spy 2005 multitool.

1 Answers  


Categories