简体   繁体   English

如何在Java中读取具有属性的xml文件?

[英]How to read xml file with attributes in java?

I am aware of SO question Failing to get element values using Element.getAttribute() but because I am java begginer, I have additional questions. 我知道SO问题无法使用Element.getAttribute()获取元素值,但是因为我是Java初学者,所以我还有其他问题。 What I am trying to build is simple application, which will read XML file and then compare it against "golden master." 我试图构建的是一个简单的应用程序,它将读取XML文件,然后将其与“黄金大师”进行比较。 My problem is: 我的问题是:

  • I have lots of different XML files, which differ in attributes 我有很多不同的XML文件,它们的属性不同
  • The XML files are relatively big. XML文件相对较大。 (810 lines of filed - hard to check it by human eye) (提起810行-人眼难以检查)

Example of file: 文件示例:

  <DocumentIdentification v="Unique_ID"/>
  <DocumentVersion v="1"/>
  <DocumentType v="P81"/>
  <SenderIdentification v="TEST-001--123456" codingScheme="A01"/>
  <CreationDateTime v="2012-10-15T13:00:00Z"/>
  <InArea v="10STS-TST------W" codingScheme="A01"/>
  <OutArea v="10YWT-AYXOP01--8" codingScheme="A01"/>
  <TimeSeries>
<Period>
 <TimeInterval v="2012-10-14T22:00Z/2012-10-15T22:00Z"/>
   <Resolution v="PT15M"/>
        <Interval>
            <Pos v="1"/>
            <Qty v="500"/>
        </Interval>
        <Interval>
            <Pos v="2"/>
            <Qty v="500"/>
        </Interval>
        <Interval>
            <Pos v="3"/>
                            <Qty v="452"/>
                    </Interval>
                     ...
                     ...
                    <Interval>
            <Pos v="96"/>
                            <Qty v="891"/>
                    </Interval>
               </Period>   
        </TimeSeries>

Applying solution from the question mentioned above does not get me much further... I realised that I can cast attributes to NamedNodeMap but I dont know how to iterate through it programatically 应用上述问题的解决方案并不能帮助我进一步……我意识到我可以将属性NamedNodeMapNamedNodeMap但是我不知道如何以编程方式对其进行迭代

Yes, I know it sounds much like "do my homework" but what I really need is at least small kick to butt, moving me in correct direction. 是的,我知道这听起来很像“做我的功课”,但我真正需要的是至少打个小脚踢,使我朝正确的方向前进。 Thanks for help 感谢帮助

The method item(int index) should help iterating through the attributes: 方法item(int index)应该有助于遍历属性:

NamedNodeMap map = getItFromSomeWhere();
int i = 0;
while ((Node node = map.item(i++)) != null) {
   // node is ith node in the named map
}

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

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