[英]Error reading from xml file in java
i have the following xml: 我有以下xml:
<Triangle>
<Color>
<Red>r-0</Red>
<Green>g-0</Green>
<Blue>b-255</Blue>
</Color>
<FillColor>
<Red>r-0</Red>
<Green>g-0</Green>
<Blue>b-255</Blue>
</FillColor>
<Position>
<X>x-12</X>
<Y>y-12</Y>
</Position>
<properties>
<Y1>v-13.0</Y1>
<X1>v-12.0</X1>
<Y2>v-15.0</Y2>
<X2>v-14.0</X2>
</properties>
</Triangle>
and i want to get values from nodes for example: the node Y1 has the element v-13.0 我想从节点获取值,例如:节点Y1的元素为v-13.0
i used this method: 我用这种方法:
Map<String, Double> m = new HashMap<String, Double>();
File xmlFile = new File("Data.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document xmlDoc = docBuilder.parse(xmlFile);
NodeList list = xmlDoc.getElementsByTagName("properties");
Node node1 = list.item(0);
Element element1 = (Element) node1;
//Y1
String s = element1.getElementsByTagName("Y1").item(0).getTextContent();
String[] temp = s.split("[-]");
m.put("Y1", Double.parseDouble(temp[1]));
//X1
s =element1.getElementsByTagName("X1").item(0).getTextContent();
temp = s.split("[-]");
m.put("X1", Double.parseDouble(temp[1]));
//Y2
s =element1.getElementsByTagName("Y2").item(0).getTextContent();
temp = s.split("[-]");
m.put("Y2", Double.parseDouble(temp[1]));
//X2
s =element1.getElementsByTagName("X2").item(0).getTextContent();
temp = s.split("[-]");
m.put("X2", Double.parseDouble(temp[1]));
when i used this method to get elements of the X1, Y1, X2, Y2 in properties tag it gives me null pointer exception in the line String s = element1.getElementsByTagName("Y1").item(0).getTextContent()
but when i used the same method to get elements of X, Y in the position tag it worked. 当我使用此方法在属性标签中获取X1,Y1,X2,Y2的元素时,它在行String s = element1.getElementsByTagName("Y1").item(0).getTextContent()
给了我空指针异常当我使用相同的方法获取位置标记中的X,Y元素时,它起作用了。 what is the solution? 解决办法是什么?
Works fine on my computer. 在我的计算机上工作正常。
map : {Y1=13.0, X1=12.0, Y2=15.0, X2=14.0}
Is it possible that you are testing with the wrong xml file ? 是否有可能使用错误的xml文件进行测试? Hint: try specifying the full absolute system path to your "data.xml" file. 提示:尝试指定“ data.xml”文件的完整绝对系统路径。 You may find out that java used a different file. 您可能会发现java使用了另一个文件。
PS: not really relevant, but it's better to use Double.valueOf(String)
to convert the strings. PS:不太相关,但是最好使用Double.valueOf(String)
转换字符串。 The parseDouble
method results in a primitive double
, which requires java to perform auto-boxing. parseDouble
方法产生一个原始double
,它需要java才能执行自动装箱。 The valueOf
on the other hand immediately creates a wrapper Double
object (no auto-boxing necessary). 另一方面, valueOf
将立即创建包装Double
对象(无需自动装箱)。
Another thing to check: xml tags are case sensitive. 要检查的另一件事:xml标记区分大小写。 According to your java code, your <properties>
tag has to be in lower-case. 根据您的Java代码,您的<properties>
标记必须小写。 Your code will not work if your xml file contains a <Properties>
node instead. 如果您的xml文件包含一个<Properties>
节点,则您的代码将不起作用。
i change String[] temp ;//= s.split("[-]"); 我更改String [] temp; // = s.split(“ [-]”);
to String[] temp = s.split("[-]"); 到String [] temp = s.split(“ [-]”);
and work fine with me 和我一起工作
我的XML文件中有很多形状,每个形状都有属性标记,这就是为什么它会给我错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.