I Read xml is used to store and transfer data can you
explain it with sample code for storing and displaying data.
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 |
Can a root element type be explicitly declared in the dtd?
How do I change a text file to xml?
Using XSLT, how would you extract a specific attribute from an element in an XML document?
What does it mean to subscribe to a blog?
What is xml web service?
How dtd is different from xml?
Why was xml created?
What is the relevance of elementformdefault attribute in the schema?
What is an atom syndication feed?
Does anyone still use rss?
What is the difference between wsdl and xsd?
Briefly explain the xml spy 2005 multitool.