JAVA,Xml解析

发布于 2025-01-05 18:34:10 字数 760 浏览 2 评论 0 原文

我需要一些帮助来阅读 xml 文档。

我有一个类 Person,我想从该 xml 创建一个列表

,该 xml 类似于:

<root>
<field1></field1>
<field2></field1>
<field3></field1>
<Persons>
<id></id>
<List>
<Person>
<Name>...</Name>
<LastName>...</LastName>
</Person>
<Person>
<Name>...</Name>
<LastName>...</LastName>
</Person>
<Person>
<Name>...</Name>
<LastName>...</LastName>
</Person>
</List
</Persons>
<field4></field1>
<field5></field1>
<field6></field1>

</root>

我正在使用 dom 解析器(org.w3c.dom)

谁能告诉我获取人员信息的最佳方法是什么?

谢谢

I need some help reading an xml document.

I got a Class Person and i want create a List from that xml

the xml is something like:

<root>
<field1></field1>
<field2></field1>
<field3></field1>
<Persons>
<id></id>
<List>
<Person>
<Name>...</Name>
<LastName>...</LastName>
</Person>
<Person>
<Name>...</Name>
<LastName>...</LastName>
</Person>
<Person>
<Name>...</Name>
<LastName>...</LastName>
</Person>
</List
</Persons>
<field4></field1>
<field5></field1>
<field6></field1>

</root>

i'm using dom parser (org.w3c.dom)

Can anyone please sohw me what's the best way to get the Persons information ?

Thanks

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

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

发布评论

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

评论(6

○愚か者の日 2025-01-12 18:34:10

如果你只想读取信息,你最好(在加载 DOM 之后)使用 XPath。 XPath 存在于 J2SE API 中。如果您需要特殊示例,请写下来。

If you want only read info, you'd better (after loading DOM) use XPath on it. XPath is present in J2SE API. Write if you need special examples.

九公里浅绿 2025-01-12 18:34:10

您必须使用 Simple API for XML (SAX)。您还可以使用 XML 流 API (StaX)(教程)。

You have to use Simple API for XML (SAX). You may also use Streaming API for XML (StaX) (tutorial).

厌倦 2025-01-12 18:34:10

我更喜欢JAXB。它也存在于 J2SE API 中。

如果您需要帮助,请写下来。

I prefer JAXB. Its also present in the J2SE API.

Write if you need help.

情泪▽动烟 2025-01-12 18:34:10

我不想把这个留在这里,但我在此处回答了类似的问题。

在 Java 中,您有很多实际解析 XML 的选项 - XPath 是最慢的,但为您提供了一种很好的表达式语言来查询内容。 DOM 将是第二慢的,但会为您提供一个树模型来记住您要行走的文档。 SAX 会更快,但需要您在动态解析文档时构建列表,最后 STAX 将是最快的,但要求您根据您的格式编写一些特定代码来构建列表。

最后,我会推荐一个我编写的名为 SJXP 的库,它为您提供 STAX 的性能和 XPath 的易用性……它是两者的完美结合。

您编写诸如“/root/Persons/list/Person/Name”之类的规则,并将您的文档提供给它,每次它遇到名称时它就会触发,并为您调用用户提供的回调,将它找到的名称交给您。

您为您想要的所有值创建一些规则,中提琴...您可以为“/root/Persons/list/Person”开放标记创建 START_TAG 规则,并创建一个新的“Person p = new Person() “在你的代码中,然后当每个子元素命中时,你只需为该人设置适当的值,如下所示(作为示例):

IRule linkRule = new DefaultRule(Type.CHARACTER, "/root/Persons/list/Person/Name") {
    @Override
    public void handleParsedCharacters(XMLParser parser, String text, Object userObject) {
        // Get the last person we added on open-tag.
        Person p = personList.get(personList.size() - 1);

        // <Name> tag was parsed, 'text' is our parsed Name. Set it.
        p.setName(text);
    }
}

SJXP 的好处是内存开销低于其他解析器方法,并且性能更高(SAX 将解析匹配上的元素,基于 STAX 的解析不会将元素从流中解析出来,直到它们被请求)。

您最终会编写同样令人困惑的代码,只是为了遍历 DOM 和所有 Node 元素来构建列表。

最后,如果您对 XML-> 对象映射感到满意,您可以按照其他人的说法并利用 JAXB。您需要为您的 XML 文件编写一个模式,然后它会为您生成完美映射到它们的 Java 对象。然后,您可以将 XML 文件直接映射到 Java 对象,并调用“persons.getList()”之类的内容或 JAXB 为您生成的任何内容。

在这种情况下,内存开销和性能将与 DOM 解析相当(大致)。

I hate to just leave this here, but I answered a similar question here.

In Java you have quite a few options on actually parsing the XML - XPath will be the slowest but gives you a nice expression language to query the content with. DOM will be the second slowest but give you a tree-model in memory of your doc to walk. SAX will be faster, but requires you build the list as it parses through the doc on the fly and lastly STAX will be the fastest, but requires that you write some specific code to your format to build your list out.

Lastly, I would recommend a library I wrote called SJXP that gives you the performance of STAX with the ease of XPath... it is the perfect blend of the two.

You write rules like "/root/Persons/list/Person/Name" and give it your doc and it will fire every time it hits a name and call a user-provided callback for you, handing you the name it found.

You create a few rules for all the values you want and viola... you can create a START_TAG rule for the "/root/Persons/list/Person" open-tag, and create a new "Person p = new Person()" in your code, then as every sub-element hits, you just set the appropriate value on the person, something like this (as an example):

IRule linkRule = new DefaultRule(Type.CHARACTER, "/root/Persons/list/Person/Name") {
    @Override
    public void handleParsedCharacters(XMLParser parser, String text, Object userObject) {
        // Get the last person we added on open-tag.
        Person p = personList.get(personList.size() - 1);

        // <Name> tag was parsed, 'text' is our parsed Name. Set it.
        p.setName(text);
    }
}

The nice thing about SJXP is that the memory overhead is lower than the other parser approaches and performance higher (SAX will parse the elements on a match, STAX-based parsing doesn't parse the elements out of the stream until they are requested).

You will end up writing equally confusing code just to traverse your DOM and all the Node elements to build your list.

LASTLY, if you felt comfortable with XML->Object mapping, you could do what another person said and leverage JAXB. You will need to write a schema for your XML files, then it will generate Java objects for you that map perfect to them. Then you can just map your XML file directly to your Java object and call something like "persons.getList()" or whatever JAXB generates for you.

The memory overhead and performance will be on par with DOM parsing in that case (roughly).

淤浪 2025-01-12 18:34:10

XPath 是解决方案之一,

如果您不想使用另一个库...

那么尝试定义 DTD 并使用 ID 参数,大多数解析器都有 getElementById(ID) 功能

XPath is one of the solution,

if you do not want to use another library...

Than try defining the DTD and using the ID parameter, most of the parsers have getElementById(ID) funciton

表情可笑 2025-01-12 18:34:10

另一种简单的方法是使用正则表达式:

Pattern pattern = Pattern.compile("<Person>.*?<Name>(.*?)</Name>.*?<LastName>(.*?)</LastName>.*?</Person>", Pattern.MULTILINE | Pattern.DOTALL);
Matcher matcher = pattern.matcher(xml);
while (matcher.find())
{
  String name = matcher.group(1);
  String lastName = matcher.group(2);
}

将姓名和姓氏存储在您自己的人员数据结构中。

将 Pattern.compile 命令定义为方法外部的常量,因为它需要时间进行初始化。

请参阅
http://docs.oracle.com/javase /6/docs/api/java/util/regex/Pattern.html

Another easy way is to use regular expressions:

Pattern pattern = Pattern.compile("<Person>.*?<Name>(.*?)</Name>.*?<LastName>(.*?)</LastName>.*?</Person>", Pattern.MULTILINE | Pattern.DOTALL);
Matcher matcher = pattern.matcher(xml);
while (matcher.find())
{
  String name = matcher.group(1);
  String lastName = matcher.group(2);
}

Store the name and lastName in your own Persons-Datastructure.

Define the Pattern.compile command as a constant outside your method because it needs time for initialization.

Please see
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

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