如何使用JAXB转换Java对象中的xml数据?

如何使用JAXB转换Java对象中的xml数据?,java,xml,spring,xsd,xjc,Java,Xml,Spring,Xsd,Xjc,我是JAXB转换方面的新手。我的标记以开头,但我需要从标记中的访问数据。任何确切的建议或帮助我,如果有什么问题 此外,我也尝试了很多转换,但没有得到确切的解决方案 1. 拉杰什 浦那 412411 2. 阿金亚 孟买 412504 我的java代码是- File file = new File("/home/raj/Desktop/info.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(NewDataSet.class);

我是JAXB转换方面的新手。我的标记以
开头,但我需要从
标记中的
访问数据。任何确切的建议或帮助我,如果有什么问题

此外,我也尝试了很多转换,但没有得到确切的解决方案


1.
拉杰什
浦那
412411
2.
阿金亚
孟买
412504

我的java代码是-

File file = new File("/home/raj/Desktop/info.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(NewDataSet.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
NewDataSet dType = (NewDataSet) unmarshaller.unmarshal(file);
获取以下异常:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"DataSet"). Expected elements are <{}NewDataSet>
javax.xml.bind.UnmarshaleException:意外元素(uri:,local:“DataSet”)。预期的要素是

只是想帮忙。如果在从xml转换后已经有了结果的精确类数据。请您阅读dzon的这篇好文章:

以下是将xml转换为数据类的示例代码:

    File file = new File("product.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Product.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    product = (Product) unmarshaller.unmarshal(file);
    System.out.println(product);
如果它是自定义xml,那么我认为您必须使用一些HTMLDOM来手动转换它。下面是使用html dom的示例:

以下是在java中使用xml解析dom的示例代码:

//Get Document Builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

//Build Document
Document document = builder.parse(new File("employees.xml"));

//Normalize the XML Structure; It's just too important !!
document.getDocumentElement().normalize();

//Here comes the root node
Element root = document.getDocumentElement();
System.out.println(root.getNodeName());

//Get all employees
NodeList nList = document.getElementsByTagName("employee");
System.out.println("============================");

for (int temp = 0; temp < nList.getLength(); temp++)
{
 Node node = nList.item(temp);
 System.out.println("");    //Just a separator
 if (node.getNodeType() == Node.ELEMENT_NODE)
 {
    //Print each employee's detail
    Element eElement = (Element) node;
    System.out.println("Employee id : "    + eElement.getAttribute("id"));
    System.out.println("First Name : "  + eElement.getElementsByTagName("firstName").item(0).getTextContent());
    System.out.println("Last Name : "   + eElement.getElementsByTagName("lastName").item(0).getTextContent());
    System.out.println("Location : "    + eElement.getElementsByTagName("location").item(0).getTextContent());
 }
}
//获取文档生成器
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
//生成文档
documentdocument=builder.parse(新文件(“employees.xml”);
//规范XML结构;这太重要了!!
document.getDocumentElement().normalize();
//下面是根节点
元素根=document.getDocumentElement();
System.out.println(root.getNodeName());
//让所有员工
NodeList nList=document.getElementsByTagName(“员工”);
System.out.println(“==============================================”);
对于(int-temp=0;temp
我不明白你到底想说什么@M.Deinum?你是否理解我的问题,如果不理解,请在此发表评论。我会尽量详细解释。我不太会问这个问题。你只在这里转储了一个XSD,而你甚至没有尝试过(至少你的问题中没有显示)处理传入的XML。Ok@M.Deinum,你的意思是我应该添加我的java代码,以及我尝试过处理这些XML数据的代码。实际上,如果我添加所有代码,它将是一个大文件,所以我直接问了这个问题。可能有人有一些好的建议。我使用以下命令生成了类:xml中给定的xsd
xjc-dsrc-pcom.test.jaxb.beans test.xsd