I would use JAXB, see example here http://www.mkyong.com/java/jaxb-hello-world-example/
Answer from Evgeniy Dorofeev on Stack OverflowHow to read values from xml and store it in hash map in java - Stack Overflow
java - how to parse xml to hashmap? - Stack Overflow
How to convert XML to java.util.Map and vice versa? - Stack Overflow
Parsing XML into a Hashmap Java - Stack Overflow
You should use hash map when you dont know the fields
The rite way for you problem is to build a pojo class like this
public class MyPojo
{
private Entities Entities;
public Entities getEntities ()
{
return Entities;
}
public void setEntities (Entities Entities)
{
this.Entities = Entities;
}
@Override
public String toString()
{
return "ClassPojo [Entities = "+Entities+"]";
}
}
public class Entities
{
private String TotalResults;
private Entity[] Entity;//you can use List<> insted
public String getTotalResults ()
{
return TotalResults;
}
public void setTotalResults (String TotalResults)
{
this.TotalResults = TotalResults;
}
public Entity[] getEntity ()
{
return Entity;
}
public void setEntity (Entity[] Entity)
{
this.Entity = Entity;
}
@Override
public String toString()
{
return "ClassPojo [TotalResults = "+TotalResults+", Entity = "+Entity+"]";
}
}
I have made 2 pojos for your better understanding
you can create the rest as related to xml. Later you can just use
File file = new File("My.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(MyPojo.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
MyPojo myPojo = (MyPojo) jaxbUnmarshaller.unmarshal(file);
System.out.println(myPojo);//get your value with getter setter.
//Description, id and name can be retrieved.
In general, you use a Collection (List, Map, Set) to store objects with similar characteristics, that's why generics exist.
You can visit this tutorial for parsing the XML file in java XML Parsing In Java
Then you just need to store in a hashmap.
Underscore-java library has a static method U.fromXmlMap(xmlstring). Live example
import com.github.underscore.U;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Object> map = U.fromXmlMap(
"<Details>\r\n" +
" <detail-a>\r\n" +
"\r\n" +
" <detail> attribute 1 of detail a </detail>\r\n" +
" <detail> attribute 2 of detail a </detail>\r\n" +
" <detail> attribute 3 of detail a </detail>\r\n" +
"\r\n" +
" </detail-a>\r\n" +
"\r\n" +
" <detail-b>\r\n" +
" <detail> attribute 1 of detail b </detail>\r\n" +
" <detail> attribute 2 of detail b </detail>\r\n" +
"\r\n" +
" </detail-b>\r\n" +
"\r\n" +
"\r\n" +
"</Details>");
System.out.println(map);
// {Details={detail-a={detail=[ attribute 1 of detail a , attribute 2 of detail a , attribute 3 of detail a ]},
// detail-b={detail=[ attribute 1 of detail b , attribute 2 of detail b ]}}, #omit-xml-declaration=yes}
}
}
Use JAXB to read from xml and save it to a custom object.
Custom object class:
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "Details")
@XmlType(propOrder = { "detailA", "detailB" })
public class Details {
private List<String> detailA;
private List<String> detailB;
public void setDetailA(List<String> detailA) {
this.detailA = detailA;
}
@XmlElementWrapper(name = "detail-a")
@XmlElement(name = "detail")
public List<String> getDetailA() {
return detailA;
}
public void setDetailB(List<String> detailB) {
this.detailB = detailB;
}
@XmlElementWrapper(name = "detail-b")
@XmlElement(name = "detail")
public List<String> getDetailB() {
return detailB;
}
}
Extract the data from your xml into the object, then add contents to a map as desired:
public static void main(String[] args) throws JAXBException, FileNotFoundException {
System.out.println("Output from our XML File: ");
JAXBContext context = JAXBContext.newInstance(Details.class);
Unmarshaller um = context.createUnmarshaller();
Details details = (Details)um.unmarshal(new FileReader("details.xml"));
List<String> detailA = details.getDetailA();
List<String> detailB = details.getDetailB();
Map<String, String[]> map = new HashMap<String, String[]>();
map.put("detail-a", detailA.toArray(new String[detailA.size()]));
map.put("detail-b", detailB.toArray(new String[detailB.size()]));
for (Map.Entry<String, String[]> entry : map.entrySet()) {
//key "detail a" value={"attribute 1 of detail a","attribute 2 of detail a","attribute 3 of detail a"}
System.out.print("Key \"" +entry.getKey()+"\" value={");
for(int i=0;i<entry.getValue().length;i++){
if(i!=entry.getValue().length-1){
System.out.print("\""+entry.getValue()[i]+"\",");
}
else{
System.out.print("\""+entry.getValue()[i]+"\"}");
}
}
System.out.println();
}
}
Output will be:
Output from our XML File:
Key "detail-a" value={"attribute 1 of detail a","attribute 2 of detail a","attribute 3 of detail a"}
Key "detail-b" value={"attribute 1 of detail b","attribute 2 of detail b"}
As a note: this will work only for the xml you provided as input in your question, if you need to add more details like detail-c and so on you must define them in your custom object as well.
XML used:
<?xml version="1.0" encoding="utf-8"?>
<Details>
<detail-a>
<detail>attribute 1 of detail a</detail>
<detail>attribute 2 of detail a</detail>
<detail>attribute 3 of detail a</detail>
</detail-a>
<detail-b>
<detail>attribute 1 of detail b</detail>
<detail>attribute 2 of detail b</detail>
</detail-b>
</Details>
XStream!
Updated: I added unmarshal part as requested in comments..
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("name","chris");
map.put("island","faranga");
XStream magicApi = new XStream();
magicApi.registerConverter(new MapEntryConverter());
magicApi.alias("root", Map.class);
String xml = magicApi.toXML(map);
System.out.println("Result of tweaked XStream toXml()");
System.out.println(xml);
Map<String, String> extractedMap = (Map<String, String>) magicApi.fromXML(xml);
assert extractedMap.get("name").equals("chris");
assert extractedMap.get("island").equals("faranga");
}
public static class MapEntryConverter implements Converter {
public boolean canConvert(Class clazz) {
return AbstractMap.class.isAssignableFrom(clazz);
}
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
AbstractMap map = (AbstractMap) value;
for (Object obj : map.entrySet()) {
Map.Entry entry = (Map.Entry) obj;
writer.startNode(entry.getKey().toString());
Object val = entry.getValue();
if ( null != val ) {
writer.setValue(val.toString());
}
writer.endNode();
}
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
Map<String, String> map = new HashMap<String, String>();
while(reader.hasMoreChildren()) {
reader.moveDown();
String key = reader.getNodeName(); // nodeName aka element's name
String value = reader.getValue();
map.put(key, value);
reader.moveUp();
}
return map;
}
}
}
Here the converter for XStream including unmarshall
public class MapEntryConverter implements Converter{
public boolean canConvert(Class clazz) {
return AbstractMap.class.isAssignableFrom(clazz);
}
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
AbstractMap<String,String> map = (AbstractMap<String,String>) value;
for (Entry<String,String> entry : map.entrySet()) {
writer.startNode(entry.getKey().toString());
writer.setValue(entry.getValue().toString());
writer.endNode();
}
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
Map<String, String> map = new HashMap<String, String>();
while(reader.hasMoreChildren()) {
reader.moveDown();
map.put(reader.getNodeName(), reader.getValue());
reader.moveUp();
}
return map;
}
Assume your xml file is "c:/test.xml"
Then use the following code to read and put into a hash map in in the following format as you said
key=Roomnumber value=CoordLt,CoordLn
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.spi.DirStateFactory.Result;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class xml {
public static void main(String[] args)
{
HashMap<String,String>hMap=new HashMap<String, String>();
File file=new File("c:/test.xml");
if(file.exists())
{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder builder = factory.newDocumentBuilder();
Document document=builder.parse(file);
Element documentElement=document.getDocumentElement();
NodeList sList=documentElement.getElementsByTagName("section");
if (sList != null && sList.getLength() > 0)
{
for (int i = 0; i < sList.getLength(); i++)
{
Node node = sList.item(i);
if(node.getNodeType()==Node.ELEMENT_NODE)
{
Element e = (Element) node;
NodeList nodeList = e.getElementsByTagName("Room");
String roomName= nodeList.item(0).getChildNodes().item(0)
.getNodeValue();
nodeList = e.getElementsByTagName("CoordLt");
String coordValues= nodeList.item(0).getChildNodes().item(0)
.getNodeValue();
nodeList = e.getElementsByTagName("CoordLn");
coordValues=coordValues+","+ nodeList.item(0).getChildNodes().item(0)
.getNodeValue();
hMap.put(roomName, coordValues);
}
}
}
} catch(Exception e){
System.out.println("exception occured");
}
}else
{
System.out.println("File not exists");
}
}
}
If you transform the XML using this xslt (which can be done in Java) you get your desired output, If someone sle knows howto load in a hashmap you'll be fine.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no" omit-xml-declaration ="yes" />
<xsl:template match="/sections">
<xsl:apply-templates select="section"/>
</xsl:template>
<xsl:template match="section" xml:space="default">
<xsl:apply-templates select="Room"/>
<xsl:text>:</xsl:text>
<xsl:apply-templates select="CoordLt" />
<xsl:text>,</xsl:text>
<xsl:apply-templates select="CoordLn"/>
<xsl:text>;</xsl:text>
</xsl:template>
</xsl:stylesheet>
Is there an good package to let me do it? Otherwise I will have to write a recursive method on my own, which is time consuming and error prone. Thanks!
The program below will parse all of your XML into a Map.
It first constructs a document from the XML...
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new ByteArrayInputStream(XML.getBytes()));
(Here, XML is just a string constant containing all of the XML you put in your question)
You can then get a list of all the "Application" nodes in the document...
NodeList applicationNodes = doc.getElementsByTagName("Application");
You will then iterate over each of these doing the following...
- Get the child nodes of the application element
- Find the name and email from the child nodes
- Put these values in a map
Code
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Stack {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new ByteArrayInputStream(XML.getBytes()));
Map<String, String> map = readFromDocument(doc);
for(Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
private static Map<String, String> readFromDocument(Document doc) {
Map<String, String> map = new HashMap<>();
NodeList applicationNodes = doc.getElementsByTagName("Application");
for(int k = 0; k < applicationNodes.getLength(); k++) {
Node applicationNode = applicationNodes.item(k);
NodeList subNodes = applicationNode.getChildNodes();
String key = null;
String value = null;
for(int j = 0; j < subNodes.getLength(); j++) {
Node node = subNodes.item(j);
if("Name".equals(node.getNodeName())) {
key = node.getTextContent();
} else if("StatusIO-Email".equals(node.getNodeName())) {
value = node.getTextContent();
}
}
if(key == null || value == null) {
throw new IllegalStateException("Could not find all attributes of node, key=" + key + ", value=" + value);
}
map.put(key, value);
}
return map;
}
private static final String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<Domains>" +
" <Domain name=\"Parts\">" +
" <Applications>" +
" <Application>" +
" <Name>Paragon</Name>" +
" <StatusIO-Email> component+d6bb9f42-814c-40de-bb3d-c67c8972c8d2@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>PartsAssistanceCenter</Name>" +
" <StatusIO-Email>component+066ac96e-c038-477a-b477-9b17d8183a61@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>PartsPricingFeedback</Name>" +
" <StatusIO-Email>component+d34dd6d3-b75c-4777-9ed2-99db85dc591d@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" </Applications>" +
" </Domain>" +
" <Domain name=\"Admin\">" +
" <Applications>" +
" <Application>" +
" <Name>DealerAgreementSystem</Name>" +
" <StatusIO-Email>component+bc3e0990-fa84-4e5f-8ca5-25cec10e82a1@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>DealerFinancialSystem</Name>" +
" <StatusIO-Email> component+50c88a69-b630-465e-9538-d42184489987@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>DealerPersonnelMaintencance</Name>" +
" <StatusIO-Email>component+74ac5cdb-77d8-4b45-8ead-fe2aa981d8fb@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>DealerWebSiteContentManagement</Name>" +
" <StatusIO-Email>component+7b794d21-b74c-4a07-afcf-baab0a9d7384@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" </Applications>" +
" </Domain>" +
" <Domain name=\"Sales\">" +
" <Applications>" +
" <Application>" +
" <Name>C3RemarketingProgram</Name>" +
" <StatusIO-Email>component+5a19286d-94de-483c-83f2-8f26bcb9eb2e@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>CARFAX</Name>" +
" <StatusIO-Email>component+672dc29d-9990-499c-a9b2-1284032dab62@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>CertificateProgram</Name>" +
" <StatusIO-Email>component+9a331d90-7576-4b3c-b7bc-f04fe7f43a81@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>CompetitorInformation</Name>" +
" <StatusIO-Email>component+ede2b266-b8fb-426f-a2dd-cfa03f91ae22@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>CustomizedDelivery</Name>" +
" <StatusIO-Email>component+b5a9668f-8097-4904-8973-f1fa3c914f52@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>DE1</Name>" +
" <StatusIO-Email> component+6262e08b-0781-47a6-ac68-1ef36500610b@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>DistributionAndLogistics</Name>" +
" <StatusIO-Email>component+89e75ea9-bc54-4c7a-978a-b638241c407a@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>DistributionandLogistics-Sprinter</Name>" +
" <StatusIO-Email>component+668f00bc-dea4-4a56-8d99-c4bfc7de6410@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>EuropeanDeliveryProgram</Name>" +
" <StatusIO-Email>component+69c8aa70-4fed-4946-97fd-6f08e0128f87@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>ExtendedLimitedWarranty</Name>" +
" <StatusIO-Email>component+5ea8d40a-2c5b-438f-9c23-198e1c78c247@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>ExtendedLimitedWarrantyVans</Name>" +
" <StatusIO-Email>component+c14c627d-ec5c-4974-ab88-b4f201f48c48@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>F&IPro</Name>" +
" <StatusIO-Email> component+bdf17e22-f4e9-4e21-a5b6-a8c25eb72aa0@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>FleetInfo-DealerSite</Name>" +
" <StatusIO-Email>component+e2c43815-fcc4-4538-bde3-3f41d763d57a@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>FleetSales-CustomerSite</Name>" +
" <StatusIO-Email>component+3805c27a-d2b2-4de5-86e6-d6c35d12ee16@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>GeographicLinesets</Name>" +
" <StatusIO-Email> component+94ee1578-1ff1-498f-900a-d6750b55a74f@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>KnownExporterSearch</Name>" +
" <StatusIO-Email>component+fb403be8-14b1-4485-851e-c3b0c93a905a@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>ManheimAuctions</Name>" +
" <StatusIO-Email>component+0d9e6bdb-ea6a-48a5-ae9f-c496dad09782@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>MarketSupportSystem</Name>" +
" <StatusIO-Email>component+864d733f-c9b5-4a66-a67f-94b10a964022@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>MBAdvantage</Name>" +
" <StatusIO-Email>component+6301fa14-81a6-4823-aa99-66c0d1244169@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>MBBusDevCenter</Name>" +
" <StatusIO-Email>component+7a46434f-143d-4ddc-ad36-099cea6cfac5@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>mbrace-ElectronicSubscriberAgreement</Name>" +
" <StatusIO-Email>component+47c62d30-406f-44e8-a55c-6780cc2ec016@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>MercedesProgramInfo</Name>" +
" <StatusIO-Email>component+d5cab941-5e62-4134-8e8e-c31f2d3fe5be@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>PrepaidMaintenance</Name>" +
" <StatusIO-Email>component+feb2791b-6c76-4047-a068-d989b32c14e2@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>PrepaidMaintenanceVans</Name>" +
" <StatusIO-Email>component+115bee7c-6863-4311-806f-ddb485f4e2a5@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>RDAResourceManager</Name>" +
" <StatusIO-Email>component+a7024b6c-be83-4814-84ad-e23b9ca7dc30@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>VehicleConfigurator</Name>" +
" <StatusIO-Email>component+5beda28e-dfd2-422e-83cb-ca98226db630@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>VehicleWraps</Name>" +
" <StatusIO-Email>component+b02e0618-c013-434c-9142-aacc907a7231@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" </Applications>" +
" </Domain>" +
" <Domain name=\"Service\">" +
" <Applications>" +
" <Application>" +
" <Name>ASRALocal</Name>" +
" <StatusIO-Email>component+704833c3-c7ef-488c-a84e-6818652a9993@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>MarketResearchAfterSales</Name>" +
" <StatusIO-Email> component+1008863b-df94-4422-b7d0-24e754c8f92e@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>MOC1TabletSolutions</Name>" +
" <StatusIO-Email>component+10a3086f-da72-45ea-8cec-ec12d1aa7fae@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>OperationsGuidelines</Name>" +
" <StatusIO-Email>component+952684d7-8561-4ab8-9268-776dcae72c85@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>smartTekInfo</Name>" +
" <StatusIO-Email>component+b52b1e02-51a3-48b7-a16b-ba2000cf1c8c@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>smartWarrantyManual</Name>" +
" <StatusIO-Email>component+a23c655a-1f0c-4af3-880c-636fb39fe334@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>SeatProtection</Name>" +
" <StatusIO-Email>component+29b6c2a0-3a72-4934-8f1e-ecd674503552@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>ServiceandPartsSalesTools</Name>" +
" <StatusIO-Email>component+a5226dfc-beeb-4b75-8609-09518ad5140d@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>SmallRepair</Name>" +
" <StatusIO-Email>component+892f5cb6-e8aa-4608-9d1f-d97197e37304@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>STARTekInfo</Name>" +
" <StatusIO-Email>component+cac881e6-ef80-4b80-a135-ba559ed1abfe@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" <Application>" +
" <Name>WarrantyManual</Name>" +
" <StatusIO-Email>component+1f2c68d9-45d5-4eba-9533-1fe31441c44b@notifications.statuspage.io</StatusIO-Email>" +
" </Application>" +
" </Applications>" +
" </Domain>" +
"</Domains>";
}
Output
MarketResearchAfterSales = component+1008863b-df94-4422-b7d0-24e754c8f92e@notifications.statuspage.io
WarrantyManual = component+1f2c68d9-45d5-4eba-9533-1fe31441c44b@notifications.statuspage.io
F&IPro = component+bdf17e22-f4e9-4e21-a5b6-a8c25eb72aa0@notifications.statuspage.io
ASRALocal = component+704833c3-c7ef-488c-a84e-6818652a9993@notifications.statuspage.io
MarketSupportSystem = component+864d733f-c9b5-4a66-a67f-94b10a964022@notifications.statuspage.io
ExtendedLimitedWarrantyVans = component+c14c627d-ec5c-4974-ab88-b4f201f48c48@notifications.statuspage.io
CustomizedDelivery = component+b5a9668f-8097-4904-8973-f1fa3c914f52@notifications.statuspage.io
DistributionandLogistics-Sprinter = component+668f00bc-dea4-4a56-8d99-c4bfc7de6410@notifications.statuspage.io
MBBusDevCenter = component+7a46434f-143d-4ddc-ad36-099cea6cfac5@notifications.statuspage.io
RDAResourceManager = component+a7024b6c-be83-4814-84ad-e23b9ca7dc30@notifications.statuspage.io
OperationsGuidelines = component+952684d7-8561-4ab8-9268-776dcae72c85@notifications.statuspage.io
CertificateProgram = component+9a331d90-7576-4b3c-b7bc-f04fe7f43a81@notifications.statuspage.io
PrepaidMaintenance = component+feb2791b-6c76-4047-a068-d989b32c14e2@notifications.statuspage.io
mbrace-ElectronicSubscriberAgreement = component+47c62d30-406f-44e8-a55c-6780cc2ec016@notifications.statuspage.io
DealerPersonnelMaintencance = component+74ac5cdb-77d8-4b45-8ead-fe2aa981d8fb@notifications.statuspage.io
ServiceandPartsSalesTools = component+a5226dfc-beeb-4b75-8609-09518ad5140d@notifications.statuspage.io
MBAdvantage = component+6301fa14-81a6-4823-aa99-66c0d1244169@notifications.statuspage.io
DealerAgreementSystem = component+bc3e0990-fa84-4e5f-8ca5-25cec10e82a1@notifications.statuspage.io
CARFAX = component+672dc29d-9990-499c-a9b2-1284032dab62@notifications.statuspage.io
smartWarrantyManual = component+a23c655a-1f0c-4af3-880c-636fb39fe334@notifications.statuspage.io
DealerWebSiteContentManagement = component+7b794d21-b74c-4a07-afcf-baab0a9d7384@notifications.statuspage.io
MOC1TabletSolutions = component+10a3086f-da72-45ea-8cec-ec12d1aa7fae@notifications.statuspage.io
PartsAssistanceCenter = component+066ac96e-c038-477a-b477-9b17d8183a61@notifications.statuspage.io
ExtendedLimitedWarranty = component+5ea8d40a-2c5b-438f-9c23-198e1c78c247@notifications.statuspage.io
FleetSales-CustomerSite = component+3805c27a-d2b2-4de5-86e6-d6c35d12ee16@notifications.statuspage.io
ManheimAuctions = component+0d9e6bdb-ea6a-48a5-ae9f-c496dad09782@notifications.statuspage.io
Paragon = component+d6bb9f42-814c-40de-bb3d-c67c8972c8d2@notifications.statuspage.io
MercedesProgramInfo = component+d5cab941-5e62-4134-8e8e-c31f2d3fe5be@notifications.statuspage.io
STARTekInfo = component+cac881e6-ef80-4b80-a135-ba559ed1abfe@notifications.statuspage.io
C3RemarketingProgram = component+5a19286d-94de-483c-83f2-8f26bcb9eb2e@notifications.statuspage.io
DE1 = component+6262e08b-0781-47a6-ac68-1ef36500610b@notifications.statuspage.io
EuropeanDeliveryProgram = component+69c8aa70-4fed-4946-97fd-6f08e0128f87@notifications.statuspage.io
PartsPricingFeedback = component+d34dd6d3-b75c-4777-9ed2-99db85dc591d@notifications.statuspage.io
SmallRepair = component+892f5cb6-e8aa-4608-9d1f-d97197e37304@notifications.statuspage.io
DistributionAndLogistics = component+89e75ea9-bc54-4c7a-978a-b638241c407a@notifications.statuspage.io
GeographicLinesets = component+94ee1578-1ff1-498f-900a-d6750b55a74f@notifications.statuspage.io
SeatProtection = component+29b6c2a0-3a72-4934-8f1e-ecd674503552@notifications.statuspage.io
FleetInfo-DealerSite = component+e2c43815-fcc4-4538-bde3-3f41d763d57a@notifications.statuspage.io
PrepaidMaintenanceVans = component+115bee7c-6863-4311-806f-ddb485f4e2a5@notifications.statuspage.io
CompetitorInformation = component+ede2b266-b8fb-426f-a2dd-cfa03f91ae22@notifications.statuspage.io
DealerFinancialSystem = component+50c88a69-b630-465e-9538-d42184489987@notifications.statuspage.io
VehicleWraps = component+b02e0618-c013-434c-9142-aacc907a7231@notifications.statuspage.io
VehicleConfigurator = component+5beda28e-dfd2-422e-83cb-ca98226db630@notifications.statuspage.io
smartTekInfo = component+b52b1e02-51a3-48b7-a16b-ba2000cf1c8c@notifications.statuspage.io
KnownExporterSearch = component+fb403be8-14b1-4485-851e-c3b0c93a905a@notifications.statuspage.io
One way could be to use XPath to get all elements Application then for each of them get the value of the tag Name and StatusIO-Email by calling getTextContent() on the first element returned by getElementsByTagName:
// Parse the data
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(myFile);
// Create the XPath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/Domains/Domain/Applications/Application");
// Evaluate the XPath expression to get all elements Application
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
Map<String, String> map = new HashMap<>();
// For each element Application get the value of Name and StatusIO-Email
for (int i = 0; i < nl.getLength(); i++) {
// Cast it first as an element
Element node = (Element) nl.item(i);
map.put(
node.getElementsByTagName("Name").item(0).getTextContent(),
node.getElementsByTagName("StatusIO-Email").item(0).getTextContent()
);
}
NB: This code assumes that we always have a tag Name and StatusIO-Email in each element Application if not you will need to check first for each tag if it exists.