Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

public class Main {
    public static Object parseRequestObjectFromSoap(String soapXml) throws Exception {
        Object result = null;

        if (soapXml != null && soapXml.trim().length() > 0) {
            DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
            xmlFact.setNamespaceAware(true);

            DocumentBuilder builder = xmlFact.newDocumentBuilder();
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            StringReader xsr = new StringReader(soapXml);
            InputSource is = new InputSource(xsr);
            Document doc = builder.parse(is);

            //Node requestNode = (Node) xpath.evaluate("/soap:Envelope/soap:Body/*", doc, XPathConstants.NODE);
            Node requestNode = (Node) xpath.evaluate("/*[local-name()='Envelope']/*[local-name()='Body']/*", doc,
                    XPathConstants.NODE);

            JAXBContext ctx = JAXBContext.newInstance("xo.xoi.orderentry.ebonding");
            Unmarshaller unmarshaller = ctx.createUnmarshaller();
            result = unmarshaller.unmarshal(requestNode);
        }

        return result;
    }
}