Generate java file from XML file without any API [closed]

妖精的绣舞 提交于 2019-12-10 12:26:12

问题


How can I generate Java file, means generate name of classes methods attributes, without using any API from XML

my XML file : Source

<class name="person">

<Attribut type="int">Age</Attribut>
<Attribut type="String">Name</Attribut>
</class>

to java file:

 public class person {
    int age;
    String Name;
 }

Your help is very appreciated

Thank you


回答1:


If you still want to do it your own. You might have a look on this simplified example.

Warnings first:

  • it lacks proper Exception handling
  • it is build only to work with your proposed XML example

The sample code

import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class XMLStreamReaderDemo {

    public static void main(String[] args) throws Exception {
        String xmlFileName = "source.xml";
        StringBuilder javaSource = transform(xmlFileName);
        System.out.println(javaSource);
    }

    static StringBuilder transform(String xmlFileName) throws
            FactoryConfigurationError, IOException, XMLStreamException {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader parser = null;
        StringBuilder source = new StringBuilder();
        try (FileInputStream inputStream = new FileInputStream(xmlFileName)) {
            parser = factory.createXMLStreamReader(inputStream);
            while (parser.hasNext()) {
                switch (parser.getEventType()) {
                    case XMLStreamConstants.START_ELEMENT:
                        processStartElement(parser, source);
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        processCharacters(parser, source);
                        break;

                    case XMLStreamConstants.END_ELEMENT:
                        processEndElement(parser, source);
                        break;
                    default:
                        break;
                }
                parser.next();
            }
        } finally {
            if (parser != null) {
                parser.close();
            }
        }
        return source;
    }

    static void processEndElement(XMLStreamReader reader, StringBuilder sb) {
        String element = reader.getLocalName();
        if ("class".equals(element)) {
            sb.append("}");
        } else if ("Attribut".equals(element)) {
            sb.append(";\n");
        }
    }

    static void processCharacters(XMLStreamReader reader, StringBuilder sb) {
        if (!reader.isWhiteSpace()) {
            sb.append(" ").append(reader.getText());
        }
    }

    static void processStartElement(XMLStreamReader reader, StringBuilder sb) {
        String element = reader.getLocalName();
        if ("class".equals(element)) {
            sb.append("public class ")
                    .append(reader.getAttributeValue(0))
                    .append(" {\n");
        } else if ("Attribut".equals(element)) {
            sb.append("    ")
                    .append(reader.getAttributeValue(0));
        }
    }
}

Assuming source.xml contains

<class name="person">
<Attribut type="int">Age</Attribut>
<Attribut type="String">Name</Attribut>
</class>

the code prints

public class person {
    int Age;
    String Name;
}

The "only" thing left for you to do: implement all the missing parts. If this still contains "too much" XML API ... well ... write you own parser. ;-)




回答2:


You can generate java classes from xsd or xml file without using any java code or class file use jaxb https://jaxb.java.net/ or a exe file default provided by java located in jdk bin folder xjc.exe

documentation at https://docs.oracle.com/javase/8/docs/technotes/tools/unix/xjc.html



来源:https://stackoverflow.com/questions/35270490/generate-java-file-from-xml-file-without-any-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!