java - 如何使用 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>
解决方案
只是想帮忙。如果您在从 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());
}
}
推荐阅读
- javascript - 用于更改主题的 React Native Context
- python - 为什么 Pygame 文本打印没有完全覆盖最后一个文本打印,并且在文本周围留下奇怪的光环?
- sql - Postgres:jsonb对象的多个条件
- javascript - 单击特定的 h3 标签后如何更改视频的来源?
- amazon-web-services - AWS HTTP API 网关:句柄 /
- android - 使用 Java 11 运行 sonarqube-gradle-plugin:3.3 时出错
- flutter - 按钮不出现
- jquery - 图例分页器在窗口调整大小时错误显示
- python - 使用 Python 检测并可能定位图像中的勾选框(光学标记识别)
- pyspark - Pyspark:withColumn 查找列,但条件告诉它不要这样做