-6

I would like to know how to get data from XML file into my java code. For this i would be creating my XML files and and then will be writing a java code. Then i'll try to fetch data from XML to java. For this some mapping has to be done in my java code or i have to use some java API. Kindly help with your knowledge or provide me some link for studying!

Rahul Mishra
  • 27
  • 1
  • 1
  • 1

3 Answers3

3

You can read XML in as a String and parse it using a built in SAX or DOM parser.

You can use JAXB to map XML directly into your objects.

Your choice. But you need to do some Googling and study.

duffymo
  • 305,152
  • 44
  • 369
  • 561
2

You can use DOM XML Parser (JAXP). The following example is taken from mkyong

<company>
<staff id="1001">
    <firstname>yong</firstname>
    <lastname>mook kim</lastname>
    <nickname>mkyong</nickname>
    <salary>100000</salary>
</staff>
<staff id="2001">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
</staff>
</company>

The java code:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class ReadXMLFile {

   public static void main(String argv[]) {

       try {
        File fXmlFile = new File("/Users/mkyong/staff.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile); 
        //optional, but recommended
        //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize(); 
        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        NodeList nList = doc.getElementsByTagName("staff");
        System.out.println("----------------------------");

            for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
                System.out.println("\nCurrent Element :" + nNode.getNodeName());
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) nNode; 
        System.out.println("Staff id : " + eElement.getAttribute("id"));
        System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
        System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
        System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
        System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

            }
        }
       } catch (Exception e) {
        e.printStackTrace();
       }
    }

}

fareed
  • 3,034
  • 6
  • 37
  • 65
0

You can also use Jaxb2Marshaller from Spring. First you need to create the Jaxb2Marshaller bean in your Spring context specifying the classes to be bound for the marshaller and the schemas where the XML is defined:

<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.example.Employee</value>
            <value>com.example.Department</value>
        </list>
    </property>
    <property name="schemas">
        <list>
            <value>classpath:schemas/schema.xsd</value>
        </list>
    </property>
</bean>

In your code, get the string XML and use the marshaller to unmarshal the object. For example:

@Autowire
private Jaxb2Marshaller marshaller;

...

final Employee employee = (Employee) marshaller.unmarshal(new StreamSource(new StringReader(xmlString)));
Eduardo Sanchez-Ros
  • 1,777
  • 2
  • 18
  • 30