2017-06-02 98 views
1

我想使用一些庫將XSD文件轉換爲XML文件。我在coderanch上看到了這個代碼並想到了嘗試。我在代碼中得到一些解析錯誤。在Java中XSD到XML解析錯誤

這裏是代碼: -

package csvconverter; 

import java.io.File; 
import java.io.StringReader; 

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.ws.jaxme.xs.XSParser; 
import org.apache.xerces.impl.xs.psvi.XSModel; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 

import jlibs.xml.sax.XMLDocument; 
import jlibs.xml.xsd.XSInstance; 

public interface xsdtoxml { 
    public static void main(String[] pArgs) { 
      try { 
       String filename = "out.xsd"; 
       // instance. 

       final Document doc = loadXsdDocument(filename); 

          //Find the docs root element and use it to find the targetNamespace 
       final Element rootElem = doc.getDocumentElement(); 
       String targetNamespace = null; 
       if (rootElem != null && rootElem.getNodeName().equals("xs:schema")) 
          { 
        targetNamespace = rootElem.getAttribute("targetNamespace"); 
       } 


          //Parse the file into an XSModel object 
       org.apache.xerces.xs.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, "out"); 
       XMLDocument sampleXml = new XMLDocument(new StreamResult(System.out), true, 4, null); 
       instance.generate(xsModel, rootElement, sampleXml); 
      } 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); 
      } catch (final Exception e) { 
       e.printStackTrace(); 
       // throw new ContentLoadException(msg); 
      } 

      return doc; 
     } 

} 

我收到這條線的問題 -

org.apache.xerces.xs.XSModel xsModel = new XSParser().parse(filename); 

當我改變字符串的InputSource,那麼它不接受。 請指出我在代碼中丟失了什麼。

這是異常錯誤我得到: -

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method parse(InputSource) in the type XSParser is not applicable for the arguments (String) 

    at csvconverter.xsdtoxml.main(xsdtoxml.java:40) 

回答

1

充分利用jlibs庫xsparser就能解決問題

org.apache.xerces.xs.XSModel xsModel = new jlibs.xml.xsd.XSParser().parse("po.xsd");