有沒有可以從java類生成xsd模式的庫?谷歌產生了很多結果相反(從XSD的Java類)。Pojo to xsd generation
8
A
回答
8
JAXB 2.0允許您從註釋的Java類創建XML模式。
您會在AMIS blog和JavaPassion site處找到一些示例。
3
JiBX執行此
架構發生器工具首先讀取 一個或多個JiBX綁定定義 ,然後使用反射來解釋 在綁定中引用的Java類 的結構。通過將綁定定義與 組合,實際的類信息模式生成器能夠構造一個或多個XML模式來表示由綁定處理的文檔。
3
這裏是我會怎麼做:
public void pojoToXSD(JAXBContext context, Object pojo, OutputStream out)
throws IOException, TransformerException
{
final List<DOMResult> results = new ArrayList<DOMResult>();
context.generateSchema(new SchemaOutputResolver() {
@Override
public Result createOutput(String ns, String file)
throws IOException {
DOMResult result = new DOMResult();
result.setSystemId(file);
results.add(result);
return result;
}
});
DOMResult domResult = results.get(0);
Document doc = (Document) domResult.getNode();
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(out);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
}
+0
pojo對象在哪裏去? – FazoM 2017-03-22 11:30:34
-1
感謝給你的方法。只是想添加完整的例子。
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import test.Test;
public class Main {
public static void main(String[] args) throws JAXBException,
FileNotFoundException {
JAXBContext context = JAXBContext.newInstance("test");
try {
new Main().pojoToXSD(context, new Test(), System.out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void pojoToXSD(JAXBContext context, Object pojo, OutputStream out)
throws IOException, TransformerException
{
final List<DOMResult> results = new ArrayList<DOMResult>();
context.generateSchema(new SchemaOutputResolver() {
@Override
public Result createOutput(String ns, String file)
throws IOException {
DOMResult result = new DOMResult();
result.setSystemId(file);
results.add(result);
return result;
}
});
DOMResult domResult = results.get(0);
com.sun.org.apache.xerces.internal.dom.DocumentImpl doc = com.sun.org.apache.xerces.internal.dom.DocumentImpl) domResult.getNode();
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(out);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
}
}
//---------- below will go in test package
package test;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
private final static QName _Test_QNAME = new Name("urn:vertexinc:enterprise:calendar:1:0", "Test");
public ObjectFactory() {
}
public Test createTest() {
return new Test();
}
}
package test;
public class Test {
String name;
String cls;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCls() {
return cls;
}
public void setCls(String cls) {
this.cls = cls;
}
}
相關問題
- 1. 生成沒有.xsd的POJO?
- 2. Pojo to Json,如果Pojo嵌入對象
- 3. XSD TO XML轉換
- 4. Google Direction JSON to POJO(Jackson)
- 5. Hibernate工具Pojo Generation的用法默認設置
- 6. 從POJO(不帶JAXB)生成XSD?
- 7. Xsd with nonlatin elements to java
- 8. linq to xml或xsd方法
- 9. NineBlock generation
- 10. Pdf generation in ionic2
- 11. Setter generation for List using JAXB
- 12. Javascript Date Generation programatically
- 13. Spark - Random Number Generation
- 14. Procedural Island Generation confusion
- 15. iOS tone generation
- 16. Number generation going wrong
- 17. dom generation with ajax
- 18. Generation pdf truncated
- 19. JHipster Application Generation Stuck
- 20. Procedural World Generation
- 21. PHP Image Generation
- 22. cmsms rss generation
- 23. Procedural Avatar Generation
- 24. Jasper Reports - Docx Generation
- 25. VirtueMart - No pdf generation
- 26. Express Url Generation
- 27. JPA ID auto_increment generation
- 28. Eclipse Code Generation Hook
- 29. thumbnail generation/image resize
- 30. OPENCV_PYTHON_IMAGE2VIDEO GENERATION錯誤
我應該是JAXB專家,我不知道。多麼尷尬。先生,先生+1。 – skaffman 2009-08-25 08:45:47
有一些谷歌fu參與:)沒有理由在那裏感到驚訝。 – 2009-08-25 12:05:24
很感謝。我的谷歌功能很弱。 – Surya 2009-08-25 19:46:07