-
Notifications
You must be signed in to change notification settings - Fork 1
Quick Start
SuiFei edited this page Jun 2, 2025
·
1 revision
本指南将帮助您在5分钟内开始使用XSD2Code,快速体验从XSD到代码的转换过程。
- Go 1.21 或更高版本
- 基本的命令行操作知识
- 一个XSD文件(我们提供示例)
# 克隆仓库
git clone https://github.com/suifei/xsd2code.git
cd xsd2code
# 构建工具
go build -o xsd2code cmd/main.go从 GitHub Releases 下载适合您操作系统的预构建版本。
我们提供了一个简单的示例XSD文件:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/simple"
xmlns:tns="http://example.com/simple"
elementFormDefault="qualified">
<!-- 简单的人员信息类型 -->
<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
<xs:element name="age" type="xs:int" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
<!-- 根元素 -->
<xs:element name="person" type="tns:PersonType"/>
</xs:schema>将上述内容保存为 person.xsd 文件。
# 基本用法 - 生成Go代码
./xsd2code -xsd=person.xsd
# 指定输出文件
./xsd2code -xsd=person.xsd -output=person.go
# 指定包名
./xsd2code -xsd=person.xsd -output=person.go -package=models生成的Go代码示例:
package models
import (
"encoding/xml"
)
// PersonType represents PersonType
type PersonType struct {
XMLName xml.Name `xml:"http://example.com/simple PersonType"`
Name string `xml:"name"`
Email string `xml:"email"`
Age *int `xml:"age,omitempty"`
Id string `xml:"id,attr"`
}./xsd2code -xsd=person.xsd -lang=java -output=Person.java -package=com.example.models生成的Java代码示例:
package com.example.models;
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "person", namespace = "http://example.com/simple")
public class PersonType {
@XmlElement(name = "name", required = true)
private String name;
@XmlElement(name = "email", required = true)
private String email;
@XmlElement(name = "age")
private Integer age;
@XmlAttribute(name = "id", required = true)
private String id;
// Getters and setters...
}./xsd2code -xsd=person.xsd -lang=csharp -output=Person.cs -package=Example.Models# 创建简单的测试程序
cat > test_person.go << 'EOF'
package main
import (
"encoding/xml"
"fmt"
"./models" // 假设生成的代码在models包中
)
func main() {
person := models.PersonType{
Name: "张三",
Email: "zhangsan@example.com",
Age: &[]int{30}[0],
Id: "p001",
}
data, err := xml.MarshalIndent(person, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
EOF
# 运行测试
go run test_person.go./xsd2code -xsd=person.xsd -output=person.go -json生成带JSON标签的代码:
type PersonType struct {
XMLName xml.Name `xml:"http://example.com/simple PersonType" json:"-"`
Name string `xml:"name" json:"name"`
Email string `xml:"email" json:"email"`
Age *int `xml:"age,omitempty" json:"age,omitempty"`
Id string `xml:"id,attr" json:"id"`
}./xsd2code -xsd=person.xsd -validation -validation-output=validation.go项目提供了更复杂的示例XSD文件:
# 使用高级示例
./xsd2code -xsd=examples/advanced_example.xsd -output=advanced.go
# 使用带约束的示例
./xsd2code -xsd=examples/test_restrictions.xsd -output=restrictions.go现在您已经成功生成了第一个代码文件!接下来可以:
如果在快速开始过程中遇到问题:
- 检查 常见问题
- 查看 故障排除 指南
- 在 GitHub Issues 提问
- 建议在实际项目中使用前,先用简单的XSD测试工具
- 生成的代码可以直接在您的项目中使用
- 支持增量更新,XSD修改后重新生成即可
- 所有生成的代码都通过编译测试,可以放心使用
🎉 恭喜! 您已经成功完成了XSD2Code的快速开始教程!