<?xml version="1.0" encoding="utf-8"?> <Repository xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Href="https://10.158.83.250:9398/api/repositories/9dd23445-d0e5-4117-a1f2-2ce41689e639?format=Entity" Type="Repository" Name="VNET-UATVEEAMBAK" UID="urn:veeam:Repository:9dd23445-d0e5-4117-a1f2-2ce41689e639" xmlns="http://www.veeam.com/ent/v1.0"> <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" Name="VNET-UATVEEAMBAK" Type="RepositoryReference" 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> <Capacity>172622695366656</Capacity> <FreeSpace>62226719834112</FreeSpace> <Kind>DDBoost</Kind> </Repository>帮我写一个java工具类,用来解析以上格式的xml文件
时间: 2023-07-18 12:37:50 浏览: 104
脚本可以批量去除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) {
File xmlFile = new File("path/to/xml/file.xml");
Repository repository = parseXmlFile(xmlFile);
System.out.println(repository);
}
private static Repository parseXmlFile(File xmlFile) {
Repository repository = new Repository();
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
// Get repository metadata
Element repositoryElement = (Element) doc.getElementsByTagName("Repository").item(0);
repository.setName(repositoryElement.getAttribute("Name"));
repository.setUid(repositoryElement.getAttribute("UID"));
repository.setType(repositoryElement.getAttribute("Type"));
// Get repository links
List<Link> links = new ArrayList<>();
NodeList linkNodes = repositoryElement.getElementsByTagName("Link");
for (int i = 0; i < linkNodes.getLength(); i++) {
Node linkNode = linkNodes.item(i);
if (linkNode.getNodeType() == Node.ELEMENT_NODE) {
Element linkElement = (Element) linkNode;
Link link = new Link();
link.setName(linkElement.getAttribute("Name"));
link.setHref(linkElement.getAttribute("Href"));
link.setRel(linkElement.getAttribute("Rel"));
link.setType(linkElement.getAttribute("Type"));
links.add(link);
}
}
repository.setLinks(links);
// Get repository capacity and free space
Element capacityElement = (Element) doc.getElementsByTagName("Capacity").item(0);
long capacity = Long.parseLong(capacityElement.getTextContent());
repository.setCapacity(capacity);
Element freeSpaceElement = (Element) doc.getElementsByTagName("FreeSpace").item(0);
long freeSpace = Long.parseLong(freeSpaceElement.getTextContent());
repository.setFreeSpace(freeSpace);
// Get repository kind
Element kindElement = (Element) doc.getElementsByTagName("Kind").item(0);
String kind = kindElement.getTextContent();
repository.setKind(kind);
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
return repository;
}
public static class Repository {
private String name;
private String uid;
private String type;
private List<Link> links;
private long capacity;
private long freeSpace;
private String kind;
// Getters and setters omitted for brevity
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Repository [name=").append(name).append(", uid=").append(uid).append(", type=").append(type)
.append(", links=").append(links).append(", capacity=").append(capacity).append(", freeSpace=")
.append(freeSpace).append(", kind=").append(kind).append("]");
return sb.toString();
}
}
public static class Link {
private String name;
private String href;
private String rel;
private String type;
// Getters and setters omitted for brevity
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Link [name=").append(name).append(", href=").append(href).append(", rel=").append(rel)
.append(", type=").append(type).append("]");
return sb.toString();
}
}
}
```
这个类中包含了两个嵌套的类 `Repository` 和 `Link`,分别代表 XML 文件中的 `<Repository>` 和 `<Link>` 元素。`Repository` 类中包含了所有 `Repository` 元素的数据,包括名称、UID、类型、链接、容量、可用空间和种类。`Link` 类中则包含了所有 `Link` 元素的数据,包括名称、Href、Rel 和类型。在 `parseXmlFile` 方法中,我们使用了 Java 自带的 DOM 解析器来解析 XML 文件,并将解析出来的数据存储到 `Repository` 和 `Link` 对象中。最后,我们在 `main` 方法中调用了 `parseXmlFile` 方法,并将解析出来的 `Repository` 对象打印出来。
阅读全文