Dne 20.4.2011 10:19, Martin Beránek napsal(a):
eclipse takovou funkcionalitu ve svem editoru poskytuje. pravým tlačítkem na soubor 
*.xsd => generate => xml file

Cvičně jsem si napsal kód, který z XSD poslaného tazatelem vyrobí
pomocí JDOMu požadovanou zprávu. Stálo mně to asi dvě hodiny učení se XSOMu :-)

package cz.makub.xsom;
import com.sun.xml.xsom.*;
import com.sun.xml.xsom.parser.XSOMParser;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.xml.sax.SAXException;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class XSOMaJDOM {

    public static void main(String[] args) throws IOException, SAXException {
        XSOMParser parser = new XSOMParser();
        parser.parse(new File("message.xsd"));
        XSSchema schema = parser.getResult().getSchema(1);
        XSComplexType complexType = 
schema.getComplexTypes().values().iterator().next();
        System.out.println("" + complexType.getName() +" : complex");
        Element root = new Element(complexType.getName());
        for (XSParticle p1 : 
complexType.getContentType().asParticle().getTerm().asModelGroup().getChildren())
 {
            XSElementDecl el1 = p1.getTerm().asElementDecl();
            System.out.println("    " + el1.getName() +" : complex");
            Element l2Elem = new Element(el1.getName());
            root.addContent(l2Elem);
            for(XSParticle p2 : 
el1.getType().asComplexType().getContentType().asParticle().getTerm().asModelGroup().getChildren())
 {
                XSElementDecl el2 = p2.getTerm().asElementDecl();
                XSSimpleType simpleType = el2.getType().asSimpleType();
                System.out.println("        " + el2.getName()+ " : 
"+simpleType.getName());
                Element l3Elem = new Element(el2.getName());
                l2Elem.addContent(l3Elem);
                l3Elem.setText("nejaka hodnota");
            }
        }
        //output JDOM
        Document doc = new Document(root);
        BufferedOutputStream bos = new BufferedOutputStream(new 
FileOutputStream("vystup.xml"));
        XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
        out.output(doc, bos);
        bos.close();
    }
}

Tímto to považuji za vyřešené. Kdyby to XSD mohlo mít jiný počet úrovní než 
tři, tak
by stačilo použít Visitor Pattern, tedy

XSSchema schema = parser.getResult().getSchema(1);
schema.visit(new MyVisitor());

kde MyVisitor je

public class MyVisitor implements XSVisitor {
        @Override
        public void schema(XSSchema schema) {
            for (Map.Entry<String, XSComplexType> e : 
schema.getComplexTypes().entrySet()) {
                e.getValue().visit(this);
            }
        }
        @Override
        public void complexType(XSComplexType xsComplexType) {
            XSContentType xsContentType = xsComplexType.getContentType();
            XSParticle particle = xsContentType.asParticle();
            if (particle != null) {
                XSTerm term = particle.getTerm();
                if (term.isModelGroup()) {
                    for (XSParticle p : term.asModelGroup().getChildren()) {
                        XSTerm pterm = p.getTerm();
                        if (pterm.isElementDecl()) { //xs:element inside 
complex type
                            pterm.visit(this);
                        }
                    }
                }
            }
        }
//atd..
}

Makub
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Supercomputing Center Brno             Martin Kuba
Institute of Computer Science    email: [email protected]
Masaryk University             http://www.ics.muni.cz/~makub/
Botanicka 68a, 60200 Brno, CZ     mobil: +420-603-533775
--------------------------------------------------------------

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Odpovedet emailem