2

We have an application that was done in java. We need to parse not-formatted XML and convert it to Java objects.

What is the best way to do thi in java?

Thank you

<category>
    <pattern>WHAT IS MY NAME</pattern>
    <template>
        Your name is <get name="NAME" />.

        <condition name="PHONE">
            <li value="unknown">
                <condition name="EMAIL">
                    <li value="unknown">I cannot find the contact details for the name.</li>
                    <li>You can email him/her at <get name="EMAIL" />.</li>
                </condition>
            </li>
            <li>
                <condition name="EMAIL">
                    <li value="unknown">You can call him/her on <get name="PHONE" />.</li>
                    <li>You can call them on <get name="PHONE" /> or email them at <get name="EMAIL" />.</li>
                </condition>
            </li>
        </condition>
    </template>
</category>
<category>
    <pattern>WHO IS MY PARENT</pattern>
    <template>
        <templatetext>
        Your parent is $get[PARENT_CONTACT_NAME].
        You can email him/her at $get[PARENT_EMAIL].
        His/her phone number is $get[PARENT_PHONE].
        </templatetext>
    </template>
</category>
Develofer
  • 41
  • 10
  • 1
    Can you please add your not-formatted xml file. It will be easy to understand your problem. This post will help you to understand simple way to parse XML to objects in Java : https://stackoverflow.com/questions/16364547/how-to-parse-xml-to-java-object – Roma Jan 11 '19 at 04:31
  • it an AIML files.. i will add an example – Develofer Jan 11 '19 at 04:32

2 Answers2

0

I think for such complex xml structure, the best way to parse is to write a xsd schema and then transform your xml into java object.

The Below example will not give you exact solution of your problem but just an idea for parsing xml into Java Object using JAXB. Definetly this example is also not for such complex xml, but might helps you once you write a xsd schema for the same:

Here is employee.xml file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <fName>John</fName>
    <id>1</id>
    <lName>Paul</lName>
    <domain>
        <id>999</id>
        <name>CS</name>
    </domain>
</employee>

Employee.java and Domain.java :

import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer id;
    private String fName;
    private String lName;
    private Domain domain;

    public Employee() {
        super();
    }

    public Employee(int id, String fName, String lName, Domain domain) {
        super();
        this.id = id;
        this.fName = fName;
        this.lName = lName;
        this.domain = domain;
    }

    //Setters and Getters

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public Domain getDomain() {
        return domain;
    }

    public void setDomain(Domain domain) {
        this.domain = domain;
    }



    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + fName + ",lastName=" + lName + 
", department="+ domain + "]";
    }
}  

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;

@XmlRootElement(name = "domain")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Domain implements Serializable {

    private static final long serialVersionUID = 1L;


    Integer id;
    String name;

    public Domain() {
        super();
    }

    public Domain(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    //Setters and Getters
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @Override
    public String toString() {
        return "Domain [id=" + id + ", name=" + name + "]";
    }
}

JAXB to Parse XML File to Java:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;

public class ParseXMLFile {


    public static void main(String[] args) {

        File xmlFile = new File("src/employee.xml");

        JAXBContext jaxbContext;
        try
        {
            jaxbContext = JAXBContext.newInstance(Employee.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

            Employee employee = (Employee) jaxbUnmarshaller.unmarshal(xmlFile);
            System.out.println(employee);
        }
        catch (JAXBException e)
        {
            e.printStackTrace();
        }
    }


}

OUTPUT:

Employee [id=1, firstName=John,lastName=Paul, department=Domain [id=999, name=CS]]

Hope you get an idea around the things.

Roma
  • 189
  • 2
  • 14
0

This seems to be a classic example of "semi-structured" data - something half-way between pure-data and pure-document. XML handles semi-structured data much better than Java does, and XML-oriented programming languages like XSLT are much better at processing such data than Java is. So is getting the data into Java really a step in the right direction?

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • The Goal is to have a User Interface to Edit that XML.. For example the UI will have a functionality to add Category then input fields will be shown for pattern, template and so on. – Develofer Jan 11 '19 at 09:12
  • Then your first task is to define the data model that the UI will be editing. Creating an XSD schema for the XML might be a first step towards that. – Michael Kay Jan 11 '19 at 09:22