<?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-18 21:37:57 浏览: 88
脚本可以批量去除XML文件中的<?xml version=“1.0“ encoding=“utf-8“?>
以下是一个 Java 工具类,用于解析以上 XML 文件:
```java
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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 XmlParser {
public static void main(String[] args) {
try {
File file = new File("example.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("Ref");
List<Repository> repositories = new ArrayList<>();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String uid = element.getAttribute("UID");
String name = element.getAttribute("Name");
String href = element.getAttribute("Href");
Repository repository = new Repository(uid, name, href);
repositories.add(repository);
}
}
for (Repository repository : repositories) {
System.out.println(repository);
}
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
private static class Repository {
private String uid;
private String name;
private String href;
public Repository(String uid, String name, String href) {
this.uid = uid;
this.name = name;
this.href = href;
}
@Override
public String toString() {
return "Repository [uid=" + uid + ", name=" + name + ", href=" + href + "]";
}
}
}
```
这个工具类首先读取 XML 文件,然后使用 DOM 解析器解析 XML 文件,并将解析结果存储在 Repository 对象中。最后,将所有 Repository 对象打印出来。
阅读全文