helpers.XMLParser.java Source code

Java tutorial

Introduction

Here is the source code for helpers.XMLParser.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package helpers;

import Entities.ParsedSingleXML;
import Entities.XMLRow;
import java.io.IOException;
import java.io.StringReader;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/**
 *
 * @author Tomas
 */
public class XMLParser {

    public static ParsedSingleXML parseSingleDoc(String xml) {
        ParsedSingleXML doc = null;
        try {

            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(new StringReader(xml));

            Element root = document.getRootElement();
            List<Attribute> rootAttributes = root.getAttributes();
            doc = new ParsedSingleXML(root.getName());
            for (Attribute attr : rootAttributes) {
                doc.addAttr(attr.getName(), attr.getValue());
            }

            XMLRow row;

            List<Element> rootChildren = root.getChildren();
            List<Element> tempChildren;
            List<Attribute> tempAttributes;

            for (Element child : rootChildren) {

                tempChildren = child.getChildren();
                row = new XMLRow(child.getName());
                tempAttributes = child.getAttributes();
                for (Attribute attr : tempAttributes) {
                    row.addRootAttr(attr.getName(), attr.getValue());
                }

                for (Element tChild : tempChildren) {
                    row.addRowElement(tChild.getName(), tChild.getValue());
                }

                doc.addRow(row);
            }

        } catch (JDOMException ex) {
            Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
        }

        return doc;

    }

    public static LinkedList<ParsedSingleXML> parseMultipleDoc(String xml) {

        LinkedList<ParsedSingleXML> output = new LinkedList<>();
        ParsedSingleXML doc = null;
        try {

            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(new StringReader(xml));

            Element root = document.getRootElement();

            List<Element> rootChildren = root.getChildren();

            for (Element child : rootChildren) {
                System.out.println(child.toString());
                output.addLast(parseSingleDoc(child.toString()));

            }

        } catch (Exception ex) {
            //return new LinkedList<>();
        }
        return output;

    }

}