首页javajaxbJava HTML/XML - 如何在JAXB中使用Unmarshaller和模式

Java HTML/XML - 如何在JAXB中使用Unmarshaller和模式

我们想知道如何在JAXB中使用Unmarshaller和模式。
import java.io.FileInputStream;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

public class Main {

  public static void main(String[] args) throws Exception {
    JAXBContext context = JAXBContext.newInstance(User.class);

    XMLInputFactory xif = XMLInputFactory.newInstance();
    FileInputStream fis = new FileInputStream("input.xml");
    XMLStreamReader xsr = xif.createXMLStreamReader(fis);
    xsr.nextTag();
    String noNamespaceSchemaLocation = xsr.getAttributeValue(
        XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,
        "noNamespaceSchemaLocation");
    System.out.println(noNamespaceSchemaLocation);

    Unmarshaller um = context.createUnmarshaller();
    User response = (User) um.unmarshal(xsr);
  }
}

class User {
}