Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将此XML解析为HashMap_Java_Xml_Xml Parsing_Hashmap - Fatal编程技术网

Java 如何将此XML解析为HashMap

Java 如何将此XML解析为HashMap,java,xml,xml-parsing,hashmap,Java,Xml,Xml Parsing,Hashmap,如何使用Java库解析xml?下面的程序将把所有xml解析成一个映射 它首先从XML构造一个文档 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(new ByteArrayInputStream(XM

如何使用Java库解析xml?

下面的程序将把所有xml解析成一个映射

它首先从XML构造一个文档

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(new ByteArrayInputStream(XML.getBytes()));
在这里,XML只是一个字符串常量,包含您在问题中输入的所有XML

然后,您可以获得文档中所有应用程序节点的列表

NodeList applicationNodes = doc.getElementsByTagName("Application");
然后,您将重复执行以下操作中的每一项

获取应用程序元素的子节点 从子节点查找名称和电子邮件 将这些值放在地图中 密码 一种方法是使用XPath获取应用程序的所有元素,然后通过调用getElementsByTagName返回的第一个元素上的getTextContent,为每个元素获取标记名和StatusIO电子邮件的值:


注意:此代码假定每个元素应用程序中始终有一个标记名和StatusIO电子邮件,如果没有,则需要首先检查每个标记是否存在。

根据xml大小使用Dom/SAX parserdepends,或者可以使用STAX parser来解析xml,并将Name的值存储为key,StatusIO的值存储为value。请确保该名称不包含重复的值。您创建了my day@BretC!非常感谢@SaiSurya下面的答案有点好,因为它使用getElementsByTagName而不是迭代子元素…我们在哪里提供.xml文件位置?Document doc=builder.parsenew InputSourcenew StringReaderxml;在这一行中,什么是xml?是的,如果您有一个文件,它将是Document doc=builder.parsefile
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&amp;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>"; 
}
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
// 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()
    );
}