如何在Eclipse中从XSD生成XML

If you work on web services, you must have been using XSD. To test the web service, you need to generate XML from XSD file.

如果您使用Web服务 ,则必须一直在使用XSD。 要测试Web服务,您需要从XSD文件生成XML。

从XSD生成XML (Generate XML from XSD)

We can use Eclipse IDE to easily generate XML from the XSD file. Just follow the below steps to get XML from XSD.

我们可以使用Eclipse IDE轻松地从XSD文件生成XML。 只需按照以下步骤从XSD获取XML。

  1. Select XSD File in project, right click for Menu and select Generate > XML File…

    在项目中选择XSD文件,右键单击菜单,然后选择生成> XML文件…
  2. Provide the XML file Name and XML File location in the popup window. Click on next button.

    在弹出窗口中提供XML文件名和XML文件位置。 单击下一步按钮。
  3. Select the root element for which you want to generate the sample XML file, make sure to select checkboxes for “Create optional attributes” and “Create optional elements”. Below image shows how the window will look.

    选择要为其生成示例XML文件的根元素,确保选中“创建可选属性”和“创建可选元素”的复选框。 下图显示了窗口的外观。
  4. Click on Finish button and it will generate the XML File for you with the default values. Now you can change the values according to your requirement.

    单击完成按钮,它将为您生成具有默认值的XML文件。 现在,您可以根据需要更改值。

XSD到XML的示例 (XSD to XML Example)

Here is the XSD for which I will be generating XML files.

这是我将为其生成XML文件的XSD。

Employee.xsd

Employee.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="https://www.w3.org/2001/XMLSchema" 
targetNamespace="https://www.journaldev.com/Employee" 
xmlns:empns="https://www.journaldev.com/Employee" elementFormDefault="qualified">

	<element name="empRequest" type="empns:empRequest"></element>
	
	<element name="empResponse" type="empns:empResponse"></element>

	<complexType name="empRequest">
		<sequence>
			<element name="id" type="int"></element>
		</sequence>
	</complexType>
	
	<complexType name="empResponse">
		<sequence>
			<element name="id" type="int"></element>
			<element name="role" type="string"></element>
			<element name="fullName" type="string"></element>
		</sequence>
	</complexType>
</schema>

Since Employee.xsd has two root elements; empRequest and empResponse; I can generate two XML files.

由于Employee.xsd具有两个根元素; empRequestempResponse ; 我可以生成两个XML文件。

Here are the XML files generated by Eclipse, the values are changed by me.

这是Eclipse生成的XML文件,值由我更改。

EmployeeRequest.xml

EmployeeRequest.xml

<?xml version="1.0" encoding="UTF-8"?>
<empns:empRequest xmlns:empns="https://www.journaldev.com/Employee" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.journaldev.com/Employee Employee.xsd ">
  <empns:id>5</empns:id>
</empns:empRequest>

EmployeeResponse.xml

EmployeeResponse.xml

<?xml version="1.0" encoding="UTF-8"?>
<empns:empResponse xmlns:empns="https://www.journaldev.com/Employee" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.journaldev.com/Employee Employee.xsd ">
  <empns:id>1</empns:id>
  <empns:role>Developer</empns:role>
  <empns:fullName>Pankaj Kumar</empns:fullName>
</empns:empResponse>

I hope this quick tip will help you in generating XML from XSD easily.

我希望这个快速技巧可以帮助您轻松地从XSD生成XML。

翻译自: https://www.journaldev.com/821/generate-xml-xsd-eclipse-java

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值