首页 > 解决方案 > 如何使用 JAXB 转换 Java 对象中的 xml 数据?

问题描述

我是 JAXB 转换的新手。我的标签以开头,<DataSet>但我需要从标签中访问<TABLE>数据<NewDataSet>。任何确切的建议或帮助我,如果有任何问题。

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

<?xml version="1.0" encoding="utf-8"?> <DataSet xmlns="http://www.test.com"> <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="Table"> <xs:complexType> <xs:sequence> <xs:element name="ID" type="xs:decimal" minOccurs="0" /> <xs:element name="NAME" type="xs:string" minOccurs="0" /> <xs:element name="ADDRESS" type="xs:string" minOccurs="0" /> <xs:element name="PIN" type="xs:decimal" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"> <NewDataSet xmlns=""> <Table diffgr:id="Table1" msdata:rowOrder="0"> <ID>1</ID> <NAME>Rajesh</NAME> <ADDRESS>Pune</ADDRESS> <PIN>412411</PIN> </Table> <Table diffgr:id="Table2" msdata:rowOrder="1"> <ID>2</ID> <NAME>Ajinkya</NAME> <ADDRESS>Mumbai</ADDRESS> <PIN>412504</PIN> </Table> </NewDataSet> </diffgr:diffgram> </DataSet>

我的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>

标签: javaxmlspringxsdxjc

解决方案


只是想帮忙。如果您在从 xml 转换后已经有结果的确切类数据。我建议您阅读 dzon 的这篇好文章:使用 Java 使用 JAXB for XML

以下是从 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,那么我认为您必须使用一些 html dom 来手动转换它。这是使用 html dom 的示例:Java Read XML – Java DOM Parser Example

这是在 java 中使用 xml 解析的示例代码:

//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());
 }
}

推荐阅读