简体   繁体   中英

Parsing XML with multiple same name child per Element with java

this response is formatted a bit different than what I am use to . The child nodes are all part of a single element. All the children have the same names and the tags are in a generic format. I need each of these nodes to comprise a single record. The conventional program logic is not accounting for the multiple same name children. I have considered creating an additional element that is comprised of the child nodes named and then pulling the values of each node. Is this the correct method, or is there another way this type of response should be handled? Lastly, is this response formatted correctly? It seems this structure does not lend itself to parsing.

<Properties xmlns="urn:Microsoft.Search.Response.Document.Document">
     <Property>
         <Name>USERPROFILE_GUID</Name>
         <Type>String</Type>
         <Value>XXXXX97D-6XX7-XXX9-XX0A-XXXXXF0D5</Value>
     </Property>
     <Property>
         <Name>ACCOUNTNAME</Name>
         <Type>String</Type>
         <Value>APAC\johnny.good</Value>
     </Property>
     <Property>
         <Name>USERNAME</Name>
         <Type>String</Type>
         <Value>johnny.good</Value>
     </Property>
     <Property>
         <Name>PREFERREDNAME</Name>
         <Type>String</Type>
         <Value>Good, Johnny</Value>
     </Property>
     <Property>
         <Name>FIRSTNAME</Name>
         <Type>String</Type>
         <Value>Johnny</Value>
     </Property>
     <Property>
         <Name>LASTNAME</Name>
         <Type>String</Type>
         <Value>Good</Value>
     </Property>
 </Properties>

It's indeed weird to have the element names hidden as values in the <name> elements, because that is violating the very idea of XML. But basically it is a well-formed document.

I would do the same you suggest: depending on whether you need another XML or just the data in an object, I would create an xml element or java object with the appropriate element/property names and pull the values out of those property-elements. Same-name children are very common and should in no way hinder parsing.

  Use simple-xml.jar library and 
  Create 
  class Properties{
  List<Property> Property;
   }
  Class Property{
  String Name,Value,Type;

   }

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