I'm trying to read some int from a xml document:
我正在尝试从xml文档中读取一些int:
<aaa>
<agent>
<name>Agent 1</name>
<position>4 5</position>
<vector>87 78 54 5 -4</vector>
</agent>
</aaa>
That's my Java code:
这是我的Java代码:
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = documentFactory.newDocumentBuilder();
Document document = builder.parse(new File("utility.xml"));
NodeList agents = document.getElementsByTagName("agent");
for(int i=0; i<agents.getLength(); i++) {
Node node = agents.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE) {
Element agente = (Element)node;
String name = agente.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
String position = agente.getElementsByTagName("position").item(0).getFirstChild().getNodeValue();
String vector = agente.getElementsByTagName("vector").item(0).getFirstChild().getNodeValue();
I want to parse the string position into 2 Integer (4 and 5), and I want to parse the string vector into 5 Integer (and put them into an array). How can I do it? Thank you for your time!
我想将字符串位置解析为2 Integer(4和5),我想将字符串向量解析为5 Integer(并将它们放入数组中)。我该怎么做?感谢您的时间!
2 个解决方案
#1
0
The logic of converting to int[]
转换为int []的逻辑
public static void main(String args[]) {
String position ="87 78 54 5 -4";
String vector = "4 5";
String[] posArr = position.split(" ");
int[] positionArray = new int[posArr.length];
for(int i = 0 ; i < posArr.length ; i ++) {
positionArray[i] = Integer.parseInt(posArr[i]);
}
String[] vectArr = vector.split(" ");
int[] vectorArray = new int[vectArr.length];
for(int i = 0 ; i < vectArr.length ; i ++) {
vectorArray[i] = Integer.parseInt(vectArr[i]);
}
}
#2
0
You may use a Scanner
and read the contents of the String
by using nextInt
:
您可以使用Scanner并使用nextInt读取String的内容:
String stringRead = ...; //imagine you read <position> here
Scanner scanner = new Scanner(stringRead);
List<Integer> intList = new ArrayList<>();
while (scanner.hasNext()) {
intList.add(scanner.nextInt());
}
If you use Java 8, then you can make it shorter by using the power of streams:
如果您使用Java 8,那么您可以通过使用流的强大功能来缩短它:
String stringRead = ...; //imagine you read <position> here
List<Integer> intList = Arrays.stream(stringRead.split("\\s+"))
.map(x -> Integer.valueOf(x))
.collect(Collectors.toList());
#1
0
The logic of converting to int[]
转换为int []的逻辑
public static void main(String args[]) {
String position ="87 78 54 5 -4";
String vector = "4 5";
String[] posArr = position.split(" ");
int[] positionArray = new int[posArr.length];
for(int i = 0 ; i < posArr.length ; i ++) {
positionArray[i] = Integer.parseInt(posArr[i]);
}
String[] vectArr = vector.split(" ");
int[] vectorArray = new int[vectArr.length];
for(int i = 0 ; i < vectArr.length ; i ++) {
vectorArray[i] = Integer.parseInt(vectArr[i]);
}
}
#2
0
You may use a Scanner
and read the contents of the String
by using nextInt
:
您可以使用Scanner并使用nextInt读取String的内容:
String stringRead = ...; //imagine you read <position> here
Scanner scanner = new Scanner(stringRead);
List<Integer> intList = new ArrayList<>();
while (scanner.hasNext()) {
intList.add(scanner.nextInt());
}
If you use Java 8, then you can make it shorter by using the power of streams:
如果您使用Java 8,那么您可以通过使用流的强大功能来缩短它:
String stringRead = ...; //imagine you read <position> here
List<Integer> intList = Arrays.stream(stringRead.split("\\s+"))
.map(x -> Integer.valueOf(x))
.collect(Collectors.toList());