0

I am aware of SO question Failing to get element values using Element.getAttribute() but because I am java begginer, I have additional questions. What I am trying to build is simple application, which will read XML file and then compare it against "golden master." My problem is:

  • I have lots of different XML files, which differ in attributes
  • The XML files are relatively big. (810 lines of filed - hard to check it by human eye)

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

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

3
  • what exactly is the goal of comparing the two files? you might get away by doing things way easier depending on what exactly you need to achieve. Commented Oct 24, 2012 at 9:02
  • My idea is: Read the file and compare to expected values: Especially the bits with InArea and OutArea, SendersIdentification - those codes are specific to each file and should be unique to that name Commented Oct 24, 2012 at 9:20
  • in that case you are able to go with what Andreas_D somewhat suggests, the namedNodeMap can be obtained from doing node.getAttributes(), then using getNamedItem("v") to get the attribute Commented Oct 24, 2012 at 9:28

1 Answer 1

1

The method item(int index) should help iterating through the attributes:

NamedNodeMap map = getItFromSomeWhere();
int i = 0;
while ((Node node = map.item(i++)) != null) {
   // node is ith node in the named map
}
Sign up to request clarification or add additional context in comments.

3 Comments

How obvious mr Watson ... I feel completly dumb now :)
just additional question - if I do node.toString() will it print the dontent? (dumb dumb dumb)
I bet you're looking for Node#getNodeValue which will be the attributes value (if the node is an attribute). Node#getNodeName is the attributes name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.