xml parser code example

Example 1: java xml reader

package com.mkyong.seo;

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();
    }
  }

}

Example 2: xml formatter

<?xml version="1.0" encoding="UTF-8"?>
<Macro index="0" name="SuperSource Macro" description="Created with H2R SuperSource Layouts">
   <Op id="SuperSourceV2BoxEnable" superSource="0" boxIndex="0" enable="True" />
   <Op id="SuperSourceV2BoxInput" superSource="0" boxIndex="0" input="Camera1" />
   <Op id="SuperSourceV2BoxSize" superSource="0" boxIndex="0" size="0.8" />
   <Op id="SuperSourceV2BoxXPosition" superSource="0" boxIndex="0" xPosition="-7.875" />
   <Op id="SuperSourceV2BoxYPosition" superSource="0" boxIndex="0" yPosition="0.4791666666666667" />
   <Op id="SuperSourceV2BoxMaskEnable" superSource="0" boxIndex="0" enable="True" />
   <Op id="SuperSourceV2BoxMaskLeft" superSource="0" boxIndex="0" left="7" />
   <Op id="SuperSourceV2BoxMaskTop" superSource="0" boxIndex="0" top="0" />
   <Op id="SuperSourceV2BoxMaskRight" superSource="0" boxIndex="0" right="7" />
   <Op id="SuperSourceV2BoxMaskBottom" superSource="0" boxIndex="0" bottom="0" />
   <Op id="SuperSourceV2BoxEnable" superSource="0" boxIndex="1" enable="True" />
   <Op id="SuperSourceV2BoxInput" superSource="0" boxIndex="1" input="Camera2" />
   <Op id="SuperSourceV2BoxSize" superSource="0" boxIndex="1" size="0.8" />
   <Op id="SuperSourceV2BoxXPosition" superSource="0" boxIndex="1" xPosition="7.958333333333333" />
   <Op id="SuperSourceV2BoxYPosition" superSource="0" boxIndex="1" yPosition="0.2916666666666667" />
   <Op id="SuperSourceV2BoxMaskEnable" superSource="0" boxIndex="1" enable="True" />
   <Op id="SuperSourceV2BoxMaskLeft" superSource="0" boxIndex="1" left="7" />
   <Op id="SuperSourceV2BoxMaskTop" superSource="0" boxIndex="1" top="0" />
   <Op id="SuperSourceV2BoxMaskRight" superSource="0" boxIndex="1" right="7" />
   <Op id="SuperSourceV2BoxMaskBottom" superSource="0" boxIndex="1" bottom="0" />
   <Op id="SuperSourceV2BoxEnable" superSource="0" boxIndex="2" enable="False" />
   <Op id="SuperSourceV2BoxEnable" superSource="0" boxIndex="3" enable="False" />
</Macro>

Tags:

Css Example