简体   繁体   English

如何在Java中将xml形式的字符串转换为xml格式

[英]how to convert string in the form of xml to xml format in java

I have xml data in the form of string. 我有字符串形式的xml数据。 I would like to know how to convert it using Java. 我想知道如何使用Java进行转换。

In MULE ESB I have the string message in logger showing it as: 在MULE ESB中,在记录器中有字符串消息显示为:

[payload:java.lang.String] [payload:java.lang.String]

I need to store each elements and give a response in XML. 我需要存储每个元素并以XML给出响应。 How do I configure it with JAXB or any other process? 如何使用JAXB或任何其他进程对其进行配置?

<EAI_MESSAGE>
<EAI_HEADER>
                <MsgFormat>ISO8583</MsgFormat>
                <MsgVersion>1.0.1.0</MsgVersion>    
</EAI_HEADER>

<EAI_BODY>

    <FundTransferRequest>
                <CustomerId>CT1010</CustomerId>
                <SourceAccNo>12346789012345</SourceAccNo>
                <SourceAccName>SOURAV MUKHERJEE</SourceAccName>
                <SourceAccType>SAVINGS</SourceAccType>
                <SourceIFSC>SBI1010</SourceIFSC>
                <Amount>5000.00</Amount>
    </FundTransferRequest>`enter code here`
</EAI_BODY>

Your are looking in the correct direction when you mention JAXB. 提及JAXB,您的方向正确。 JDK comes with xjc which allows you to auto-generate classes that are needed to unmarshall and marshall XML files. JDK带有xjc,它允许您自动生成解组和编组XML文件所需的类。 You can follow the following steps to build a solutions 您可以按照以下步骤构建解决方案

  1. Build XML Schema for input and output XML format. 为输入和输出XML格式构建XML模式。 There are number of tools available to achieve this. 有许多工具可以实现此目的。 ( How to generate JAXB classes from just XML ) 如何仅从XML生成JAXB类
  2. Use the XML schemas to generate JAXB java library. 使用XML模式生成JAXB Java库。 ( Generate your JAXB classes in a second with xjc ) 使用xjc在一秒钟内生成您的JAXB类
  3. Now configure you java application to unmarshall (build java objects from XML) using input JAXB library. 现在,将Java应用程序配置为使用输入JAXB库解组(从XML构建Java对象)。 ( Using JAXB to Generate Java Objects from XML Document ) 使用JAXB从XML文档生成Java对象
  4. Build Java object structure using classes generated using XJC for needed for output XML format using the Java objects created by unmarshalling. 使用通过解组创建的Java对象,使用XJC生成的类构建Java对象结构,以输出XML格式所需。 This is pure Java programming task 这是纯Java编程任务
  5. Now, generate the output XML needed using marshaller in JAXB library 现在,在JAXB库中使用marshaller生成所需的输出XML

This seems complex but except for generating output Java data structure from input Java, rest of the process is straight forward. 这似乎很复杂,但是除了从输入Java生成输出Java数据结构外,其余过程很简单。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM