如何使用Java在多个级别上提取具有相同标签名称的XML标签值

时间:2019-07-08 11:57:59

标签: java xml

我想提取存在于多个子级别的父标记SuccessorPartNumber的XML值,即HardwareComponentPart以及节点-> SoftwareComponentPart。

<?xml version="1.0" encoding="UTF-8"?>
<ShowVehicleManufacturingActions>
	<ApplicationArea>
		<Sender>
			<LogicalId>IVS</LogicalId>
			<Component>MFG</Component>
			<Environment>Production</Environment>
			<Task>Build</Task>
			<ReferenceId>Manufacturing Action</ReferenceId>
			<Confirmation>N</Confirmation>
			<Language>eng</Language>
			<AuthorizationId>hgitham_csa</AuthorizationId>
		</Sender>
		<CreationDateTime>Feb-21-2019 17:30:2</CreationDateTime>
		<BODId>2015_L462_eqnxa2_2019-2-21_17-30-2.xml</BODId>
		<UserArea>
			<BODRevision>MFG 1.2</BODRevision>
			<TaskRevision>IVS 3.1</TaskRevision>
		</UserArea>
	</ApplicationArea>
	<DataArea>
		<Verb>Show</Verb>
		<Noun>VehicleManufacturingActions</Noun>
		<VehicleManufacturingActions>
			<VehicleManufacturingActionHeader>
				<VehicleProgram>
					<ProgramCode>L462</ProgramCode>
					<VehicleName>Discovery</VehicleName>
					<ModelYear>2015</ModelYear>
				</VehicleProgram>
				<ManufacturingPlant>
					<PlantName>Solihull-VCATS</PlantName>
					<Revision>A</Revision>
					<Description>Solihull Assembly Plant VCATS System</Description>
					<PlantCode>eqnxa2</PlantCode>
					<TimeZoneOffset>+0</TimeZoneOffset>
				</ManufacturingPlant>
			</VehicleManufacturingActionHeader>
			<VehicleManufacturingActionDetail>
				<PartLineage>
						<ECUAcronym>AAM</ECUAcronym>
						<ECUFlashSequence>0</ECUFlashSequence>
						<AssyPN>GX73-18C808-XC</AssyPN>
						<AsDeliveredPID>F112</AsDeliveredPID>
						<SuccessorPartNumber>GX73-18C808-XE</SuccessorPartNumber>
						<CertificationRequired>Y</CertificationRequired>
						<CertificationFulfilled>N</CertificationFulfilled>
						<ProgInSvc>Y</ProgInSvc>
						<ConfigurationMethod>ANY</ConfigurationMethod>
						<ProgrammingSpecificationResponse>01</ProgrammingSpecificationResponse>
						<ProgrammingSpecificationPID>F162</ProgrammingSpecificationPID>
						<DiagnosticSpecificationResponse>01</DiagnosticSpecificationResponse>
						<DiagnosticSpecificationPID>F163</DiagnosticSpecificationPID>
						<Compliance>
							<ComplianceFlag>False</ComplianceFlag>
						</Compliance>
							<HardwareComponentPart>
								<HardwareType>GX73-14C524-XA</HardwareType>
								<SuccessorPartNumber>GX73-14C524-XB</SuccessorPartNumber>
								<FilePNPid>FD23</FilePNPid>
								<Compliance>
									<ComplianceFlag>False</ComplianceFlag>
								</Compliance>
							</HardwareComponentPart>
						<Node>
							<NodeAddr>86</NodeAddr>
							<CPUType>Main</CPUType>
							<Protocol>MOST</Protocol>
							<Bit32>1</Bit32>
							<GatewayType>NONE</GatewayType>
							<SoftwareComponentPart>
									<PartType>Strategy</PartType>
									<FilePN>GX73-14C526-XC</FilePN>
									<SuccessorPartNumber>GX73-14C526-XE</SuccessorPartNumber>
									<FilePNPid>FD22</FilePNPid>
									<Compliance>
										<ComplianceFlag>False</ComplianceFlag>
									</Compliance>
									<ManufacturingFlashTestStatus>Not Required</ManufacturingFlashTestStatus>
									<FlashInManufacturing>N</FlashInManufacturing>
							</SoftwareComponentPart>
						</Node>
				</PartLineage>
					</VehicleManufacturingActionDetail>
		</VehicleManufacturingActions>
	</DataArea>
</ShowVehicleManufacturingActions>

JAVA:     列表ivsList = new ArrayList <>();

        String fileName = new File("").getAbsolutePath() + "/src/test/resources/Files/IVS_XML.xml";
        File path = new File(fileName);
        System.out.println(fileName);

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fileName);

        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("PartLineage");

        System.out.println("----------------------------");

        for (int temp = 0; temp < 2; temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                IvsDigitalTwinModel ivsDt = new IvsDigitalTwinModel();
                Element eElement = (Element) nNode;
                System.out.println("*** Temp ***:" + temp);                             
                    System.out.println("SuccessorPartNumber: "
                            + eElement.getElementsByTagName("SuccessorPartNumber").item(0).getTextContent());
                    ivsDt.setSuccessorPartNumber(
                            eElement.getElementsByTagName("SuccessorPartNumber").item(0).getTextContent()); 


                ivsList.add(ivsDt);
            }
        }

我要提取第一个标签而不是子标签的SuccessorPartNumber值。 如果不存在SuccessorPartNumber,它将从子节点提取值。即HardwareComponentPart或节点->可用的SoftwareComponentPart。

如果不存在第一个标记的SuccessorPartNumber,则应给出nullpointer异常或空值。

0 个答案:

没有答案