<?xml version="1.0" encoding="utf-8"?> <EntityReferences xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.veeam.com/ent/v1.0"> <Ref UID="urn:veeam:Repository:9dd23445-d0e5-4117-a1f2-2ce41689e639" Name="VNET-UATVEEAMBAK" Href="https://10.158.83.250:9398/api/repositories/9dd23445-d0e5-4117-a1f2-2ce41689e639" Type="RepositoryReference"> <Links> <Link Href="https://10.158.83.250:9398/api/backupServers/155012e7-8b5f-4dda-ad83-06eb5e559aa0" Name="10.158.83.250" Type="BackupServerReference" Rel="Up" /> <Link Href="https://10.158.83.250:9398/api/repositories/9dd23445-d0e5-4117-a1f2-2ce41689e639?format=Entity" Name="VNET-UATVEEAMBAK" Type="Repository" Rel="Alternate" /> <Link Href="https://10.158.83.250:9398/api/repositories/9dd23445-d0e5-4117-a1f2-2ce41689e639/backups" Type="BackupReferenceList" Rel="Down" /> <Link Href="https://10.158.83.250:9398/api/repositories/9dd23445-d0e5-4117-a1f2-2ce41689e639/replicas" Type="ReplicaReferenceList" Rel="Down" /> </Links> </Ref> <Ref UID="urn:veeam:Repository:c99c6c3c-c3d3-4ade-b0d8-bb65399d6a92" Name="Default Backup Repository" Href="https://10.158.83.250:9398/api/repositories/c99c6c3c-c3d3-4ade-b0d8-bb65399d6a92" Type="RepositoryReference"> <Links> <Link Href="https://10.158.83.250:9398/api/backupServers/155012e7-8b5f-4dda-ad83-06eb5e559aa0" Name="10.158.83.250" Type="BackupServerReference" Rel="Up" /> <Link Href="https://10.158.83.250:9398/api/repositories/c99c6c3c-c3d3-4ade-b0d8-bb65399d6a92?format=Entity" Name="Default Backup Repository" Type="Repository" Rel="Alternate" /> <Link Href="https://10.158.83.250:9398/api/repositories/c99c6c3c-c3d3-4ade-b0d8-bb65399d6a92/backups" Type="BackupReferenceList" Rel="Down" /> <Link Href="https://10.158.83.250:9398/api/repositories/c99c6c3c-c3d3-4ade-b0d8-bb65399d6a92/replicas" Type="ReplicaReferenceList" Rel="Down" /> </Links> </Ref> </EntityReferences>帮我写一个java工具类,用来解析以上格式的xml文件
时间: 2023-07-19 07:38:19 浏览: 173
可以使用Java自带的DOM解析器来解析XML文件。以下是一个示例工具类:
```java
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class XMLParser {
public static void parse(String filePath) {
try {
File file = new File(filePath);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);
// 获取根节点
Element root = document.getDocumentElement();
// 获取所有子节点
NodeList nodeList = root.getElementsByTagName("Ref");
for (int i = 0; i < nodeList.getLength(); i++) {
Element element = (Element)nodeList.item(i);
String uid = element.getAttribute("UID");
String name = element.getAttribute("Name");
String href = element.getAttribute("Href");
String type = element.getAttribute("Type");
System.out.println("UID: " + uid + ", Name: " + name + ", Href: " + href + ", Type: " + type);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
调用该工具类的静态方法`parse`,传入XML文件的路径即可解析XML文件并输出所有`Ref`节点的属性。例如:
```java
XMLParser.parse("example.xml");
```
输出结果为:
```
UID: urn:veeam:Repository:9dd23445-d0e5-4117-a1f2-2ce41689e639, Name: VNET-UATVEEAMBAK, Href: https://10.158.83.250:9398/api/repositories/9dd23445-d0e5-4117-a1f2-2ce41689e639, Type: RepositoryReference
UID: urn:veeam:Repository:c99c6c3c-c3d3-4ade-b0d8-bb65399d6a92, Name: Default Backup Repository, Href: https://10.158.83.250:9398/api/repositories/c99c6c3c-c3d3-4ade-b0d8-bb65399d6a92, Type: RepositoryReference
```
阅读全文