Java 在Axis服务上使用Hapi:“;Can';t创建XML文档";
我正在开发一个Axis服务来与HL7远程服务接口。我有一个类(Hl7MessageTranslator)对查询请求进行如下编码:Java 在Axis服务上使用Hapi:“;Can';t创建XML文档";,java,tomcat,axis,hapi,Java,Tomcat,Axis,Hapi,我正在开发一个Axis服务来与HL7远程服务接口。我有一个类(Hl7MessageTranslator)对查询请求进行如下编码: public Hl7MessageTranslator(...) { ... parser = new DefaultXMLParser(); } public String encodeRequest(...) throws ... HL7Exception { // prepare HL7 query request QRY_A19 qryA19 = new Q
public Hl7MessageTranslator(...)
{
...
parser = new DefaultXMLParser();
}
public String encodeRequest(...) throws ... HL7Exception
{
// prepare HL7 query request
QRY_A19 qryA19 = new QRY_A19();
...
return parser.encode(qryA19);
}
encodeRequest()方法在tomcat环境(JUnit测试)之外调用时工作正常,但从Axis服务调用时失败:
创建类时,我收到以下警告:
SLF4J: This version of SLF4J requires log4j version 1.2.12 or later. See also http://www.slf4j.org/codes.html#log4j_version
INFO - 2014-05-20 09:11:07,543 - Classe: ca.uhn.hl7v2.util.Home - Metodo: setHomeDirectory - Descrizione: hapi.home is set to C:\Programmi\eclipse-j2ee-helios\.
INFO - 2014-05-20 09:11:07,933 - Classe: ca.uhn.hl7v2.VersionLogger - Metodo: printHapiVersion - Descrizione: HAPI version is: 2.2
INFO - 2014-05-20 09:11:07,949 - Classe: ca.uhn.hl7v2.VersionLogger - Metodo: checkStructureLibraries - Descrizione: Default Structure libraries found for HL7 versions 2.5, 2.6,
WARN - 2014-05-20 09:11:08,011 - Classe: ca.uhn.hl7v2.VersionLogger - Metodo: checkDOMImplementation - Descrizione: Error occured while trying to retrieve a DOMImplementation.
java.lang.RuntimeException: java.lang.ClassCastException: org.apache.xerces.dom.DOMXSImplementationSourceImpl cannot be cast to org.w3c.dom.DOMImplementationSource
at ca.uhn.hl7v2.util.XMLUtils.getDOMImpl(XMLUtils.java:55)
at ca.uhn.hl7v2.VersionLogger.checkDOMImplementation(VersionLogger.java:44)
at ca.uhn.hl7v2.VersionLogger.init(VersionLogger.java:36)
at ca.uhn.hl7v2.DefaultHapiContext.<init>(DefaultHapiContext.java:126)
at ca.uhn.hl7v2.DefaultHapiContext.<init>(DefaultHapiContext.java:112)
at ca.uhn.hl7v2.DefaultHapiContext.<init>(DefaultHapiContext.java:103)
at ca.uhn.hl7v2.parser.Parser.<init>(Parser.java:71)
at ca.uhn.hl7v2.parser.XMLParser.<init>(XMLParser.java:89)
at ca.uhn.hl7v2.parser.DefaultXMLParser.<init>(DefaultXMLParser.java:81)
at com.avelco.integrazioni.areavasta.naar.Hl7MessageTranslator.<init>(Hl7MessageTranslator.java:105)
at com.avelco.integrazioni.areavasta.naar.NaarBridge.requestGrid(NaarBridge.java:135)
at com.avelco.integration.services.Service.requestGrid(Service.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
...
WARN - 2014-05-20 09:11:08,011 - Classe: ca.uhn.hl7v2.VersionLogger - Metodo: checkDOMImplementation - Descrizione: XML parsing and encoding as well as working with Conformance Profiles will fail.
因为它在JUnit调用时可以工作,所以它一定是一个依赖项/配置问题,但我不知道在哪里可以找到它
提前谢谢
Massimo这是一个与axis依赖项的jar冲突问题。如果您有任何单独的log4j jar,请将其移除。最后,我从这里出来了 正如侯赛因·扎瓦维(Hussein Zawawi)所说,这是一场与axis的jar冲突,原因只是Xerces,而不是log4j 似乎有些HAPI
DefaultXMLParser
方法需要org.w3c.dom
对象,但我的环境中的工厂生成了org.apache.xerces.dom
对象
不幸的是,我无法在不破坏我使用的遗留软件的情况下从项目中删除Xerces。最后,我不得不编写自己的DefaultXMLParser
实现,重写错误的方法并替换XML解析和序列化调用
public class AvXmlParser extends DefaultXMLParser
{
/*
* (non-Javadoc)
* @see ca.uhn.hl7v2.parser.DefaultXMLParser#encodeDocument(ca.uhn.hl7v2.model.Message)
*/
public Document encodeDocument(Message source) throws HL7Exception
{
String messageClassName = source.getClass().getName();
String messageName = messageClassName.substring(messageClassName.lastIndexOf('.') + 1);
org.w3c.dom.Document doc = null;
try
{
// doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
doc = XmlUtilities.emptyDom();
// Element root = doc.createElement(messageName);
Element root = doc.createElementNS("urn:hl7-org:v2xml",messageName);
doc.appendChild(root);
}
catch (Exception e)
{
throw new HL7Exception("Can't create XML document - " + e.getClass().getName(), e);
}
encode(source, doc.getDocumentElement());
return doc;
}
/**
* @param groupObject
* @param groupElement
* @throws HL7Exception
*/
private void encode(ca.uhn.hl7v2.model.Group groupObject, org.w3c.dom.Element groupElement) throws HL7Exception
{
String[] childNames = groupObject.getNames();
String messageName = groupObject.getMessage().getName();
try
{
for (int i = 0; i < childNames.length; i++)
{
Structure[] reps = groupObject.getAll(childNames[i]);
for (int j = 0; j < reps.length; j++)
{
Element childElement = groupElement.getOwnerDocument().createElement(makeGroupElementName(messageName, childNames[i]));
groupElement.appendChild(childElement);
if (reps[j] instanceof Group)
{
encode((Group) reps[j], childElement);
}
else if (reps[j] instanceof Segment)
{
encode((Segment) reps[j], childElement);
}
}
}
}
catch (DOMException e)
{
throw new HL7Exception("Can't encode group " + groupObject.getClass().getName(), e);
}
}
/*
* (non-Javadoc)
* @see ca.uhn.hl7v2.parser.XMLParser#parseStringIntoDocument(java.lang.String)
*/
protected synchronized Document parseStringIntoDocument(String message) throws HL7Exception
{
try
{
Document doc = XmlUtilities.parseString(message);
return doc;
}
catch (Exception e)
{
throw new HL7Exception("Exception parsing XML",e);
}
}
}
public类AvXmlParser扩展了DefaultXMLParser
{
/*
*(非Javadoc)
*@see ca.uhn.hl7v2.parser.DefaultXMLParser#encodeDocument(ca.uhn.hl7v2.model.Message)
*/
公共文档编码文档(消息源)引发HL7Exception
{
字符串messageClassName=source.getClass().getName();
字符串messageName=messageClassName.substring(messageClassName.lastIndexOf('.')+1);
org.w3c.dom.Document doc=null;
尝试
{
//doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
doc=XmlUtilities.emptyDom();
//元素根=doc.createElement(messageName);
元素根=doc.createElements(“urn:hl7 org:v2xml”,messageName);
doc.appendChild(根);
}
捕获(例外e)
{
抛出新的HL7Exception(“无法创建XML文档-”+e.getClass().getName(),e);
}
编码(源,doc.getDocumentElement());
退货单;
}
/**
*@param-groupObject
*@param-groupElement
*@HL7Exception
*/
私有void encode(ca.uhn.hl7v2.model.Group groupObject,org.w3c.dom.Element groupElement)引发HL7Exception
{
字符串[]childNames=groupObject.getNames();
字符串messageName=groupObject.getMessage().getName();
尝试
{
for(int i=0;i
(在我的环境中,XmlUtilities
类是一个已经可用的实用程序类)。
大多数问题都是因为我正在使用的遗留软件,而不是因为HAPI或Xerces,但我希望我的解决方案在将来会有用。谢谢,但是。。。我试着从类路径中删除log4j,但它不起作用。如果没有log4j,即使从JUnit调用,类也会失败。类路径与tomcat无关。您可以将引用保留在那里,但请确保不要将jar移动到tomcatwebappok,我会试试这个。(我的服务是专有框架的一部分,因此需要一段时间…)
public class AvXmlParser extends DefaultXMLParser
{
/*
* (non-Javadoc)
* @see ca.uhn.hl7v2.parser.DefaultXMLParser#encodeDocument(ca.uhn.hl7v2.model.Message)
*/
public Document encodeDocument(Message source) throws HL7Exception
{
String messageClassName = source.getClass().getName();
String messageName = messageClassName.substring(messageClassName.lastIndexOf('.') + 1);
org.w3c.dom.Document doc = null;
try
{
// doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
doc = XmlUtilities.emptyDom();
// Element root = doc.createElement(messageName);
Element root = doc.createElementNS("urn:hl7-org:v2xml",messageName);
doc.appendChild(root);
}
catch (Exception e)
{
throw new HL7Exception("Can't create XML document - " + e.getClass().getName(), e);
}
encode(source, doc.getDocumentElement());
return doc;
}
/**
* @param groupObject
* @param groupElement
* @throws HL7Exception
*/
private void encode(ca.uhn.hl7v2.model.Group groupObject, org.w3c.dom.Element groupElement) throws HL7Exception
{
String[] childNames = groupObject.getNames();
String messageName = groupObject.getMessage().getName();
try
{
for (int i = 0; i < childNames.length; i++)
{
Structure[] reps = groupObject.getAll(childNames[i]);
for (int j = 0; j < reps.length; j++)
{
Element childElement = groupElement.getOwnerDocument().createElement(makeGroupElementName(messageName, childNames[i]));
groupElement.appendChild(childElement);
if (reps[j] instanceof Group)
{
encode((Group) reps[j], childElement);
}
else if (reps[j] instanceof Segment)
{
encode((Segment) reps[j], childElement);
}
}
}
}
catch (DOMException e)
{
throw new HL7Exception("Can't encode group " + groupObject.getClass().getName(), e);
}
}
/*
* (non-Javadoc)
* @see ca.uhn.hl7v2.parser.XMLParser#parseStringIntoDocument(java.lang.String)
*/
protected synchronized Document parseStringIntoDocument(String message) throws HL7Exception
{
try
{
Document doc = XmlUtilities.parseString(message);
return doc;
}
catch (Exception e)
{
throw new HL7Exception("Exception parsing XML",e);
}
}
}