为每个XML子节点动态创建java对象?

发布于 2024-10-31 12:53:33 字数 1504 浏览 1 评论 0原文

我目前正在学习 Java 以及如何处理 XML 数据。我一直在学习如何使用 Java SAX 将 xml 数据解析为 java 对象。此 XML 文档可以更改并添加其他子项(例如:生日、身高...)。那么处理这个 XML 文档的最佳建议是什么?我被告知要使用这样的对象:

Object1.ID
Object1.Emp_Id
Object1.Emp_Name
...
Object2.ID
Object2.Emp_Id
Object2.Emp_Name

如果 XML 收到一个像生日这样的新子对象,那么应用程序会将其添加到对象中,如下所示:

Object1.ID
Object1.Emp_Id
Object1.Emp_Name
Object1.Birthday

有人能指出我可以动态创建新对象的正确方向吗?我可以将子节点放入其中吗?那么如果子节点要改变,我不必直接指定它?抱歉,我不确定我的解释是否正确。我正在学习 SAX 并找到了本教程,但似乎没有解释我想要做什么: 将 XML 映射到 Java 对象

谢谢!

XML 文件:

<?xml version = "1.0" ?>
<Employee-Detail>

<Employee>
<ID no="1">
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>
<Sex>Male</Sex>
<Age>25</Age>
</ID>
</Employee>

<Employee>
<ID no="2">
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>
<Sex>Male</Sex>
<Age>21</Age>
</ID>
</Employee>
</Employee-Detail>

I'm currently learning Java and how to deal with XML data. I've been learning how to use the Java SAX to parse my xml data to java objects.This XML document can change and have additional children added to it (For example: Birthday, height...). So what is the best recommendation to handle this XML document? I was told to use objects like this:

Object1.ID
Object1.Emp_Id
Object1.Emp_Name
...
Object2.ID
Object2.Emp_Id
Object2.Emp_Name

If the XML received a new child like Birthday, then the app will add it to the object as such:

Object1.ID
Object1.Emp_Id
Object1.Emp_Name
Object1.Birthday

Could someone point me to the right direction where I can dynamically create new objects like the example above that I can drop the child nodes into? So if the child nodes were to change, I don't have to directly specify it? Sorry for the noob talk, I'm not sure If I'm explaining this right. I'm learning SAX and found this tutorial, but doesn't seem to explain what I want to do: Mapping XML to Java Objects

Thank yoU!

XML file:

<?xml version = "1.0" ?>
<Employee-Detail>

<Employee>
<ID no="1">
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>
<Sex>Male</Sex>
<Age>25</Age>
</ID>
</Employee>

<Employee>
<ID no="2">
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>
<Sex>Male</Sex>
<Age>21</Age>
</ID>
</Employee>
</Employee-Detail>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃酥萝莉 2024-11-07 12:53:34

基本上有两种方法可以做到这一点。首先,您可以定义名称映射到 XML 元素的 Java 类。您可以手动完成,也可以使用像 XmlBeans 这样的工具(还有很多其他工具。)两者都是等效:您必须提前了解一些有关 XML 的信息。

另一种方法是不使用专用类,而是使用某种通用“树节点”对象,该对象具有“名称”属性、属性映射和子级列表。文档对象模型 (DOM) 是其标准版本,事实上,使用自动为您构建此树的 DOM 解析器是使用 SAX 的标准替代方案。您还可以创建自己的通用元素类,但您可能应该首先使用 DOM 尝试它,看看它如何适用于您的应用程序。

There are basically two ways to do this. First, you can define Java classes whose names map onto the XML elements. You can do it by hand, or you can use a tool like XmlBeans (there are many others.) Both are equivalent: you have to know something about the XML in advance.

The other way to do it is not to use dedicated classes, but to use some kind of generic "tree node" objects which have a "name" property, a map of attributes, and a list of children. The Document Object Model (DOM) is a standard version of this, and in fact, using a DOM parser which builds this tree for you automatically is a standard alternative to using SAX. You could also create your own generic element class, but you should probably try it using DOM first, to see how it works for your application.

琉璃梦幻 2024-11-07 12:53:34

另一个选择是 XStream:http://x-stream.github.io/。它的灵活性不如 JAXB,但它的优点是不需要太多配置。

在您的例子中,您只需创建一个类:

public class Employee {
  String Id;
  String EmpId; 
  ...
}

XStream xstream = new XStream();
xstream.alias("Employee", Employee.class);
xstream.useAttributeFor(Employee.class, "ID");
xstream.aliasField("no", Blog.class, "ID");
Employee e = (Employee)xstream.fromXML(xml);

但是,如果您的 XML 没有清晰地映射到您的对象,那就有点麻烦了。

Another option is XStream: http://x-stream.github.io/. It is less flexible than JAXB, but it has the advantage that you don't need much configuration.

In your case you'd just create a class:

public class Employee {
  String Id;
  String EmpId; 
  ...
}

XStream xstream = new XStream();
xstream.alias("Employee", Employee.class);
xstream.useAttributeFor(Employee.class, "ID");
xstream.aliasField("no", Blog.class, "ID");
Employee e = (Employee)xstream.fromXML(xml);

It is a bit of a pain, though, if your XML doesn't map cleanly to your objects.

弥繁 2024-11-07 12:53:33

我个人非常喜欢使用 JAXB。

您可以获取该示例 XML 并将其传递给 XML 架构 (XSD) 生成器。然后获取 XSD 并使用 XJC 工具生成 Java 对象。

生成 Java 对象后,您可以分别使用 Marshaller 和 Unmarshaller 类编组(保存/导出/写入)XML 或解组(加载/导入/读取)它。

曾经有一个在线 XML 到 XSD 转换器,位于 http://www. Flame-ware.com/products/xml-2-xsd/ 但目前似乎已关闭。不过,您可以免费下载 Visual Studio 的快速版本来完成同样的事情。

我喜欢这个 JAXB 教程: http://download.oracle.com /javaee/5/tutorial/doc/bnbah.html

我通常仅在我想完全控制我正在解析时解析的所有内容时才使用 SAX 解析(几乎从来没有)。

我不喜欢使用 DOM 方法,因为它需要先解析整个 XML 文档,然后才能实际对其执行任何操作,并且解析后,您需要做更多工作来配置解析器/XML 使用者以了解字段是什么是真正用它做任何事情。

至于您的动态子项,我将简单地在架构中包含表示键值对的无限数量的对象。由于为您生成了所有代码,它仍然可以节省您的时间。

XmlBeans 库与 JAXB 类似,但我更喜欢后者,因为它被引入到 Java 的核心版本中,而 XmlBeans 则没有。

I personally really enjoy using JAXB.

You can take that sample XML and pass it to a XML schema (XSD) generator. Then take the XSD and generate your Java objects using the XJC tool.

Once you have your Java objects generated, you can marshal (save/export/write) the XML or unmarshal (load/import/read) it using the Marshaller and Unmarshaller classes respectively.

There used to be an online XML to XSD converter at http://www.flame-ware.com/products/xml-2-xsd/ but it seems to be down at the moment. You can download an express version of Visual Studio for free to accomplish the same thing, though.

I like this tutorial for JAXB: http://download.oracle.com/javaee/5/tutorial/doc/bnbah.html

I usually use SAX parsing only if I want full control over everything that's parsed as I'm parsing (hardly ever).

I don't enjoy using the DOM method because it requires the entire XML document to be parsed before you can actually do anything with it and once it's parsed, you need to do more work to configure your parser/XML consumer to know what the fields are to actually do anything with it.

As for your dynamic children, I would simply include in the schema an uncapped number of objects representing key-value pairs. It'll still save you time due to all the code generated for you.

The XmlBeans library is similar to JAXB, but I prefer the latter since it was inducted into Java's core release while XmlBeans was not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文