adspace


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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Do you feel that you are a good xslt programmer? : xslt

869


Are you ready to relocate? : xslt

918


Do you feel that you have chosen the right technology xslt? : xslt

903


What are the ways to add styles to html?

997