我尝试使用java将此代码转换为XML文件。输出XML文件为空。
这是java代码: -
import java.io.File;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.stream.StreamResult;
import org.apache.xerces.xs.XSModel;
import org.apache.xmlbeans.impl.inst2xsd.util.Element;
import org.w3c.dom.Document;
import jlibs.xml.sax.XMLDocument;
import jlibs.xml.xsd.XSContentModel;
import jlibs.xml.xsd.XSInstance;
import jlibs.xml.xsd.XSParser;
public class xsdconv {
public static void main(String[] pArgs) {
try {
String filename ="C:\\Users\\Admin\\Desktop\\out.xsd";
// instance.
final Document doc = loadXsdDocument(filename);
//Find the docs root element and use it to find the targetNamespace
final org.w3c.dom.Element rootElem = doc.getDocumentElement();
String targetNamespace = null;
if (rootElem != null && rootElem.getNodeName().equals("xsd:schema"))
{
targetNamespace = rootElem.getAttribute("root");
}
//Parse the file into an XSModel object
XSModel xsModel = new XSParser().parse(filename);
//Define defaults for the XML generation
XSInstance instance = new XSInstance();
instance.minimumElementsGenerated = 1;
instance.maximumElementsGenerated = 1;
instance.generateDefaultAttributes = true;
instance.generateOptionalAttributes = true;
instance.maximumRecursionDepth = 0;
instance.generateAllChoices = true;
instance.showContentModel = true;
instance.generateOptionalElements = true;
//Build the sample xml doc
//Replace first param to XMLDoc with a file input stream to write to file
QName rootElement = new QName(targetNamespace, "root");
XMLDocument sampleXml = new XMLDocument(new StreamResult("C:/Users/Admin/Desktop/out.xml"), true, 4, null);
} catch (TransformerConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Document loadXsdDocument(String inputName) {
final String filename = inputName;
final DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setValidating(false);
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
Document doc = null;
try {
final DocumentBuilder builder = factory.newDocumentBuilder();
final File inputFile = new File(filename);
doc = builder.parse(inputFile);
System.out.println(filename);
} catch (final Exception e) {
e.printStackTrace();
// throw new ContentLoadException(msg);
}
return doc;
}
}
示例XSD文件为: -
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="header" maxOccurs="unbounded" type="xsd:normalizedString"/>
<xsd:element name="row" maxOccurs="unbounded" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
所以我想从中提取标题和行信息。它正在创建空白的XML文件。我在这段代码中缺少什么? 我认为解析工作正常。
修改 这是XSD文件中的信息,我希望XSD文件像这样创建XML。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<header>Symbol</header>
<row>NIFTY 50</row>
<row>LUPIN</row>
<header>Open</header>
<row>9,670.35</row>
<row>1,082.90</row>
</root>
答案 0 :(得分:1)
您还没有填充XML文件。您可以在以下行后添加:
XMLDocument sampleXml = new XMLDocument(new StreamResult("C:/Users/Admin/Desktop/out.xml"), true, 4, null);
类似的东西:
sampleXml.startDocument();{
sampleXml.startElement("root");{
sampleXml.addElement("header", "Symbol");
sampleXml.addElement("row","NIFTY 50");
sampleXml.addElement("row","LUPIN");
sampleXml.addElement("header", "Open");
sampleXml.addElement("row","9,670.35");
sampleXml.addElement("row","1,082.90");
}
sampleXml.endElement("root");
}
sampleXml.endDocument();
或您喜欢的任何其他内容作为默认设置。