简体   繁体   English

Java读取复杂的xml文件Java

[英]Java reading complicated xml files Java

How I can read 'people=400000' and "Lubljana" data records from this xml file in Java?我如何从 Java 中的这个 xml 文件中读取“people=400000”和“Lubljana”数据记录?

And here is the sample of xml file which I already used.这是我已经使用过的 xml 文件示例。

<?xml version="1.0"?>
<countrys>
    <country people="3500000">
        <naziv>Slovenija</naziv>
        <glavnigrad people="400000">
            <naziv>Ljubljana</naziv>
        </glavnigrad>
        <povrsina jedinica="km2">51129</povrsina>
        <moneta naziv="KM" />
    </country>
</countrys>

You basically have two options:你基本上有两个选择:

  • DOM style : The approach is to load the entire XML file into memory as a tree structure of nodes and leaves. DOM 风格:方法是将整个 XML 文件作为节点和叶子的树结构加载到内存中。 You can imagine how this would be very memory intensive for a large or complicated structure.您可以想象,对于大型或复杂的结构,这将是多么占用大量内存。

Benefits: Provides object model in Java that can be easily referenced.优点:在Java中提供了易于引用的对象模型。

Drawbacks: Memory intensive for large XML structures.缺点:大型 XML 结构需要大量内存。 Potentially slower.可能更慢。

https://howtodoinjava.com/xml/read-xml-dom-parser-example/ https://howtodoinjava.com/xml/read-xml-dom-parser-example/

  • SAX style : The approach is to scroll through the file and find the node reference directly. SAX 风格:方法是滚动文件并直接找到节点引用。

Benefits: Less memory usage, potentially faster.好处:更少的内存使用,可能更快。

Drawbacks: Potential confusion in identifying nodes and/or duplicates outside of uniform tree structure.缺点:在统一树结构之外识别节点和/或重复项时可能会混淆。

https://howtodoinjava.com/xml/sax-parser-read-xml-example/ https://howtodoinjava.com/xml/sax-parser-read-xml-example/

You should research and try each approach.您应该研究并尝试每种方法。 Other approaches are derivatives of these basics.其他方法是这些基础知识的衍生物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM