简体   繁体   中英

How to convert attributes to nodes of an xml in java?

I am parsing an XML file like

<STRUCTURE ID="EV_Se96ffb9a-df1f-44e7-a4f8-818688cf8d3b">
      <SHORT-NAME>STRUCT</SHORT-NAME>
      <LONG-NAME>Structure</LONG-NAME>
</STRUCTURE>

where I am getting the child nodes of STRUCTURE and adding it to a nodeList.

Can I have an option to add the attributes of STRUCTURE ie, ID to a nodeList ?

How to convert the attributes to a node and add it to a nodelist ? Please help me out.

I am using the DOM parsing strategy

The Node class has a method getAttributes() which returns a NamedNodeMap . Of course, only elements will return an appropriate named node map (as only elements can have attributes).

On such a NamedNodeMap you can then retrieve the attribute node via a call to getNamedItem(String) or via a call to item(int) . Note, that the return type of these methods is a Node , which - in case of attributes - will in fact be an Attr .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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