/** @param tag */
 private String makeElementNameFromHexadecimalGroupElementValues(AttributeTag tag) {
   StringBuffer str = new StringBuffer();
   str.append("HEX"); // XML element names not allowed to start with a number
   String groupString = Integer.toHexString(tag.getGroup());
   for (int i = groupString.length(); i < 4; ++i) str.append("0");
   str.append(groupString);
   String elementString = Integer.toHexString(tag.getElement());
   for (int i = elementString.length(); i < 4; ++i) str.append("0");
   str.append(elementString);
   return str.toString();
 }
 private long readItemTag() throws IOException {
   AttributeTag tag = readAttributeTag();
   // System.err.println("EncapsulatedInputStream.readItemTag: tag="+tag);
   long vl = i.readUnsigned32(); // always implicit VR form for items and delimiters
   bytesRead += 4;
   if (tag.equals(TagFromName.SequenceDelimitationItem)) {
     // System.err.println("EncapsulatedInputStream.readItemTag: SequenceDelimitationItem");
     vl = 0; // regardless of what was read
     sequenceDelimiterEncountered = true;
   } else if (!tag.equals(TagFromName.Item)) {
     throw new IOException(
         "Unexpected DICOM tag "
             + tag
             + " (vl="
             + vl
             + ") in encapsulated data whilst expecting Item or SequenceDelimitationItem");
   }
   // System.err.println("EncapsulatedInputStream.readItemTag: length="+vl);
   return vl;
 }
  /**
   * @param list
   * @param document
   * @param parent
   */
  void addAttributesFromListToNode(AttributeList list, Document document, Node parent) {
    DicomDictionary dictionary = list.getDictionary();
    Iterator i = list.values().iterator();
    while (i.hasNext()) {
      Attribute attribute = (Attribute) i.next();
      AttributeTag tag = attribute.getTag();

      String elementName = dictionary.getNameFromTag(tag);
      if (elementName == null) {
        elementName = makeElementNameFromHexadecimalGroupElementValues(tag);
      }
      Node node = document.createElement(elementName);
      parent.appendChild(node);

      {
        Attr attr = document.createAttribute("group");
        attr.setValue(HexDump.shortToPaddedHexString(tag.getGroup()));
        node.getAttributes().setNamedItem(attr);
      }
      {
        Attr attr = document.createAttribute("element");
        attr.setValue(HexDump.shortToPaddedHexString(tag.getElement()));
        node.getAttributes().setNamedItem(attr);
      }
      {
        Attr attr = document.createAttribute("vr");
        attr.setValue(ValueRepresentation.getAsString(attribute.getVR()));
        node.getAttributes().setNamedItem(attr);
      }

      if (attribute instanceof SequenceAttribute) {
        int count = 0;
        Iterator si = ((SequenceAttribute) attribute).iterator();
        while (si.hasNext()) {
          SequenceItem item = (SequenceItem) si.next();
          Node itemNode = document.createElement("Item");
          Attr numberAttr = document.createAttribute("number");
          numberAttr.setValue(Integer.toString(++count));
          itemNode.getAttributes().setNamedItem(numberAttr);
          node.appendChild(itemNode);
          addAttributesFromListToNode(item.getAttributeList(), document, itemNode);
        }
      } else {
        // Attr attr = document.createAttribute("value");
        // attr.setValue(attribute.getDelimitedStringValuesOrEmptyString());
        // node.getAttributes().setNamedItem(attr);

        // node.appendChild(document.createTextNode(attribute.getDelimitedStringValuesOrEmptyString()));

        String values[] = null;
        try {
          values = attribute.getStringValues();
        } catch (DicomException e) {
          // e.printStackTrace(System.err);
        }
        if (values != null) {
          for (int j = 0; j < values.length; ++j) {
            Node valueNode = document.createElement("value");
            Attr numberAttr = document.createAttribute("number");
            numberAttr.setValue(Integer.toString(j + 1));
            valueNode.getAttributes().setNamedItem(numberAttr);
            valueNode.appendChild(document.createTextNode(values[j]));
            node.appendChild(valueNode);
          }
        }
      }
    }
  }