Log in
updated 11:29 AM UTC, May 4, 2016

How to parse an http XML file and convert it based on your need using JAVA and JAXP

  • Written by Madhu V Rao
  • Published in Core Java

This code parses the file from this location : http://ictforu.com/images/xml/matches.xml and then writes it to a local file "converted_matches.xml"

This Java class contains all the API's required to read and create an XML using JAXP. The API's used are pretty self explanatory.

 

package allxmls;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/**
 *
 * @author M.Vasudevarao
 */
public class test
{
    private DocumentBuilder builder = null;

    public static void main(String[] args)
    {
        test main = new test();
        try{
                main.buildFile("http://ictforu.com/images/xml/matches.xml");
            }
            catch (TransformerConfigurationException ex)
            {
//add your logs here
 } catch (TransformerException ex) { //add your logs here } catch (ParserConfigurationException ex) { //add your logs here } catch (SAXException ex) {
//add your logs here } catch (IOException ex) {
//add your logs here } } public void buildFile(String fileName) throws SAXException, IOException, ParserConfigurationException, TransformerConfigurationException, TransformerException { builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(fileName); Document outputDocument = null; NodeList nodes1 = document.getDocumentElement().getChildNodes(); outputDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element element1 = outputDocument.createElement(document.getDocumentElement().getNodeName()); for (int attr = 0; attr < document.getDocumentElement().getAttributes().getLength(); attr++) { element1.setAttribute(document.getDocumentElement().getAttributes().item(attr).getNodeName(), document.getDocumentElement().getAttributes().item(attr).getTextContent()); } outputDocument.appendChild(element1); for (int i = 0; i < nodes1.getLength(); i++) { Node node_1 = nodes1.item(i); if (node_1.getNodeType() == Node.ELEMENT_NODE && !((Element) node_1).getTagName().equals("#")) { Element element2 = outputDocument.createElement(node_1.getNodeName()); element1.appendChild(element2); NodeList nodes_2 = node_1.getChildNodes(); for (int j = 0; j < nodes_2.getLength(); j++) { Node node_2 = nodes_2.item(j); if (!node_2.getNodeName().contains("#")) { Element element3 = outputDocument.createElement(node_2.getNodeName()); for (int attr = 0; attr < node_2.getAttributes().getLength(); attr++) { element3.setAttribute(node_2.getAttributes().item(attr).getNodeName(), node_2.getAttributes().item(attr).getTextContent()); } if (node_2.getFirstChild() != null) { element3.appendChild(outputDocument.createTextNode(node_2.getFirstChild().getTextContent())); } element2.appendChild(element3); if (node_2.getNodeType() == Node.ELEMENT_NODE) { NodeList nodes_3 = node_2.getChildNodes(); for (int k = 0; k < nodes_3.getLength(); k++) { Node node_3 = nodes_3.item(k); if (!node_3.getNodeName().contains("#")) { if (node_3.getNodeType() == Node.ELEMENT_NODE) { Element element4 = outputDocument.createElement(node_3.getNodeName()); if (node_3.getFirstChild() != null) { element4.appendChild(outputDocument.createTextNode(node_3.getFirstChild().getTextContent())); } element3.appendChild(element4); NodeList nodes_4 = node_3.getChildNodes(); for (int l = 0; l < nodes_4.getLength(); l++) { Node node_4 = nodes_4.item(l); if (node_4.getNodeType() == Node.ELEMENT_NODE) { Element element5 = outputDocument.createElement(node_4.getNodeName()); if (node_4.getFirstChild() != null) { element5.appendChild(outputDocument.createTextNode(node_4.getFirstChild().getTextContent())); } element4.appendChild(element5); } } } } } } } } } } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(outputDocument); StreamResult result = new StreamResult("converted_matches.xml"); transformer.transform(source, result); return; } }

 

Courtesy:

XML file format is based on xmlscores.com


Powered by Bullraider.com
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest