gpt4 book ai didi

java - 使用 XML 文档 Java 的属性创建类的实例

转载 作者:行者123 更新时间:2023-12-02 03:38:46 25 4
gpt4 key购买 nike

        import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

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;

import buildingInfo.Level;

public class XmlParser {

public static void main(String[] args) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Vector<Level> vLvl = new Vector<Level>(30);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("C:\\Users\\BeatriceGhetel\\workspace\\IPProject\\src\\configuration\\Configure.xml");
NodeList itemList = doc.getElementsByTagName("LEVEL");

for (int i = 0; i < itemList.getLength(); i++) {
Node it = itemList.item(i);
if (it.getNodeType()==Node.ELEMENT_NODE) {
Element el = (Element) it;
NodeList L = el.getChildNodes();
for (int j=0; j< L.getLength(); j++) {

Node n = L.item(j);
if (n.getNodeType()==Node.ELEMENT_NODE) {
buildingInfo.Level Lvl = new BuildingInfo.Level();
if (n.getNodeName().matches("HEIGHT")) {
Lvl.Height = Float.parseFloat(n.getTextContent());
}
System.out.println(Lvl.Height + " inaltime nivel");
Element e = (Element) n;
}
}
}
}
} catch (Exception ex){
ex.printStackTrace();
}
}
}

<BUILDING>
<LEVEL>
<LABEL> 0 </LABEL>
<HEIGHT> 2.5 </HEIGHT>
<WIDTH> 9 </WIDTH>
<LENGHT> 8 </LENGHT>
<CAPACITY> 120 </CAPACITY>
<ROOM>
<LABEL> 1 </LABEL>
<TYPE> NORMALROOM </TYPE>
<HEIGHT> 2.5 </HEIGHT>
<WIDTH> 5 </WIDTH>
<LENGTH> 5 </LENGTH>
<CAPACITY> 40 </CAPACITY>
<SPRINKLER> YES </SPRINKLER>
<SMOKEDETECTOR> YES </SMOKEDETECTOR>
<EXTINGUISHERNUMBER> 2 </EXTINGUISHERNUMBER>
<DOORNUMBER> 1 </DOORNUMBER>
<DOOR>
<ID> 1 </ID>
<TYPE > EXTERNAL </TYPE >
<CAPACITY> 2 </CAPACITY>
<POSITION> NORTH </POSITION>
</DOOR>
</ROOM>

我尝试创建一个建筑模型,从配置的 XML 文档中获取属性。我遇到的问题是,我无法区分房间的高度或门的高度,而且它也不总是采用高度,有时会采用其他值,例如长度而不是高度。我还需要弄清楚如何创建房间集合,以及如何为门等嵌套项目创建对象。

最佳答案

JAXB 是解析 XML 对象的最简单方法之一。只需定义 java 类结构来匹配 XML 结构即可发挥神奇作用。它还以更简单的方式支持集合(XML 元素数组)。我已经开发了代码来解析给定的 XML(稍微更改了 XML 以显示如何处理关卡/房间/门中的集合)。希望对您有帮助。

XML 文件 - Base.xml

<?xml version="1.0" encoding="UTF-8"?>
<BUILDING>
<LEVEL>
<LABEL>0</LABEL>
<HEIGHT>2.5</HEIGHT>
<WIDTH>9</WIDTH>
<LENGHT>8</LENGHT>
<CAPACITY>120</CAPACITY>
<ROOM>
<LABEL>1</LABEL>
<TYPE>NORMALROOM</TYPE>
<HEIGHT>2.5</HEIGHT>
<WIDTH>5</WIDTH>
<LENGTH>5</LENGTH>
<CAPACITY>40</CAPACITY>
<SPRINKLER>YES</SPRINKLER>
<SMOKEDETECTOR>YES</SMOKEDETECTOR>
<EXTINGUISHERNUMBER>2</EXTINGUISHERNUMBER>
<DOORNUMBER>1</DOORNUMBER>
<DOOR>
<ID>1</ID>
<TYPE>EXTERNAL</TYPE>
<CAPACITY>2</CAPACITY>
<POSITION>NORTH</POSITION>
</DOOR>
<DOOR>
<ID>2</ID>
<TYPE>EXTERNAL</TYPE>
<CAPACITY>1</CAPACITY>
<POSITION>SOUTH</POSITION>
</DOOR>
</ROOM>
<ROOM>
<LABEL>2</LABEL>
<TYPE>DELUXEROOM</TYPE>
<HEIGHT>2.5</HEIGHT>
<WIDTH>5</WIDTH>
<LENGTH>5</LENGTH>
<CAPACITY>80</CAPACITY>
<SPRINKLER>YES</SPRINKLER>
<SMOKEDETECTOR>YES</SMOKEDETECTOR>
<EXTINGUISHERNUMBER>2</EXTINGUISHERNUMBER>
<DOORNUMBER>1</DOORNUMBER>
<DOOR>
<ID>3</ID>
<TYPE>EXTERNAL</TYPE>
<CAPACITY>2</CAPACITY>
<POSITION>NORTH</POSITION>
</DOOR>
<DOOR>
<ID>4</ID>
<TYPE>EXTERNAL</TYPE>
<CAPACITY>1</CAPACITY>
<POSITION>SOUTH</POSITION>
</DOOR>
</ROOM>
</LEVEL>
</BUILDING>

Building.java

@XmlRootElement(name = "BUILDING")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({ Level.class })
public class Building {

@XmlElement(name = "LEVEL")
private Level levels[];

public Level[] getLevels() {
return levels;
}

public void setLevels(Level[] levels) {
this.levels = levels;
}

@Override
public String toString() {
return "Building [levels=" + Arrays.toString(levels) + "]";
}

}

Door.java

@XmlRootElement(name = "DOOR")
@XmlAccessorType(XmlAccessType.FIELD)
public class Door {

@XmlElement(name="ID")
private String id;

@XmlElement(name="TYPE")
private String type;

@XmlElement(name="CAPACITY")
private String capacity;

@XmlElement(name="POSITION")
private String position;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getCapacity() {
return capacity;
}

public void setCapacity(String capacity) {
this.capacity = capacity;
}

public String getPosition() {
return position;
}

public void setPosition(String position) {
this.position = position;
}


@Override
public String toString() {
return "Door [id=" + id + ", type=" + type + ", capacity=" + capacity
+ ", position=" + position + "]";
}
}

Level.java

@XmlRootElement(name="LEVEL")
@XmlSeeAlso({Room.class})
@XmlAccessorType(XmlAccessType.FIELD)
public class Level {

@XmlElement(name="LABEL")
private String Label;

@XmlElement(name="HEIGHT")
private String Height;

@XmlElement(name="ROOM")
private Room rooms[];

public Room[] getRoom() {
return rooms;
}

public void setRoom(Room[] room) {
this.rooms = room;
}

public String getLabel() {
return Label;
}

public void setLabel(String label) {
Label = label;
}

public String getHeight() {
return Height;
}

public void setHeight(String height) {
Height = height;
}

@Override
public String toString() {
return "Level [Label=" + Label + ", Height=" + Height + ", rooms="
+ Arrays.toString(rooms) + "]";
}
}

JAXBParser.java

public class JAXBParser {

public static void main(String args[]) throws JAXBException{
File file = new File("C:\\XMLParserDemo\\src\\Base.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Building.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Building building = (Building) jaxbUnmarshaller.unmarshal(file);
System.out.println(building);


}

Room.java

@XmlRootElement(name="ROOM")
@XmlSeeAlso({Door.class})
@XmlAccessorType(XmlAccessType.FIELD)
public class Room {

@XmlElement(name="LABEL")
private String label;

@XmlElement(name="TYPE")
private String type;

@XmlElement(name="HEIGHT")
private String height;

@XmlElement(name="WIDTH")
private String width;

@XmlElement(name="LENGTH")
private String length;

@XmlElement(name="CAPACITY")
private String capacity;

@XmlElement(name="SPRINKLER")
private String sprinkler;

@XmlElement(name="SMOKEDETECTOR")
private String smokeDet;

@XmlElement(name="EXTINGUISHERNUMBER")
private String extNum;

@XmlElement(name="DOORNUMBER")
private String doorNumber;

@XmlElement(name="DOOR")
Door doors[];

public Door[] getDoor() {
return doors;
}
public void setDoor(Door[] door) {
this.doors = door;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
public String getCapacity() {
return capacity;
}
public void setCapacity(String capacity) {
this.capacity = capacity;
}
public String getSprinkler() {
return sprinkler;
}
public void setSprinkler(String sprinkler) {
this.sprinkler = sprinkler;
}
public String getSmokeDet() {
return smokeDet;
}
public void setSmokeDet(String smokeDet) {
this.smokeDet = smokeDet;
}
public String getExtNum() {
return extNum;
}
public void setExtNum(String extNum) {
this.extNum = extNum;
}
public String getDoorNumber() {
return doorNumber;
}
public void setDoorNumber(String doorNumber) {
this.doorNumber = doorNumber;
}

public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}

@Override
public String toString() {
return "Room [label=" + label + ", type=" + type + ", height=" + height
+ ", width=" + width + ", length=" + length + ", capacity="
+ capacity + ", sprinkler=" + sprinkler + ", smokeDet="
+ smokeDet + ", extNum=" + extNum + ", doorNumber="
+ doorNumber + ", doors=" + Arrays.toString(doors) + "]";
}

}

关于java - 使用 XML 文档 Java 的属性创建类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37104399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com