[Castor] 是一个 Java 的 XML binding 的工具,可以在 XML 和 Java Object 之间很方便地互相转换。现在在使用中发现了一个问题,如下所示:
-
mapping.xml
-
-
<mapping>
-
<class name="com.Parent">
-
<map-to xml="parent" />
-
<field name="children" type="com.Child" collection="map">
-
<bind-xml name="child">
-
</bind-xml>
-
</field>
-
</class>
-
-
<class name="com.Child" identity="id">
-
<map-to xml="child" />
-
<field name="id" type="java.lang.Integer">
-
<bind-xml name="id" node="attribute">
-
</bind-xml>
-
</field>
-
</class>
-
</mapping>
-
-
test.xml
-
-
<?xml version="1.0"?>
-
-
<parent>
-
<child id="1"/>
-
<child id="2"/>
-
</parent>
-
-
Parent.java :
-
-
package com;
-
-
import javax.xml.xpath.XPathFactory;
-
import javax.xml.xpath.XPath;
-
import javax.xml.xpath.XPathExpression;
-
import javax.xml.xpath.XPathConstants;
-
-
import javax.xml.parsers.DocumentBuilder;
-
import javax.xml.parsers.DocumentBuilderFactory;
-
-
import org.xml.sax.InputSource;
-
-
import org.w3c.dom.Document;
-
import org.w3c.dom.Node;
-
import org.w3c.dom.Element;
-
-
import org.exolab.castor.xml.Unmarshaller;
-
import org.exolab.castor.mapping.Mapping;
-
import org.exolab.castor.mapping.MappingException;
-
-
import java.io.InputStream;
-
import java.util.Map;
-
import java.util.HashMap;
-
-
public class Parent {
-
-
private HashMap<Integer, Child> children;
-
-
public Map<Integer, Child> getChildren() {
-
return children;
-
}
-
-
public void setChildren(Map<Integer, Child> children) {
-
this.children = new HashMap<Integer, Child>(children);
-
}
-
-
public void test() {
-
try {
-
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-
dbf.setValidating(false);
-
dbf.setNamespaceAware(true);
-
-
XPathFactory factory = XPathFactory.newInstance();
-
XPath xpath = factory.newXPath();
-
-
InputStream is = getClass().getResourceAsStream("/m.xml");
-
Mapping mapping = new Mapping(getClass().getClassLoader());
-
mapping.loadMapping(new InputSource(is));
-
Unmarshaller unmar = new Unmarshaller(mapping);
-
unmar.setIgnoreExtraElements(true);
-
-
InputStream stream = getClass().getResourceAsStream("/t.xml");
-
DocumentBuilder builder = dbf.newDocumentBuilder();
-
Document doc = builder.parse(stream, "parent");
-
doc.getDocumentElement().normalize();
-
-
String exprStr = "/parent";
-
XPathExpression expr = xpath.compile(exprStr);
-
Node node = (Node)expr.evaluate(doc, XPathConstants.NODE);
-
-
Parent parent = (Parent)unmar.unmarshal((Element)node);
-
System.err.println(parent.getChildren());
-
for (Map.Entry entry : parent.getChildren().entrySet()) {
-
System.err.println("key class = " + entry.getKey().getClass());
-
}
-
-
Child child = parent.getChildren().get(1);
-
// Child with id 1 should be returned.
-
System.err.println("child 1 = " + child);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
public static void main(String[] argv) {
-
Parent parent = new Parent();
-
parent.test();
-
}
-
}
-
-
Child.java :
-
-
package com;
-
-
public class Child {
-
private int id;
-
-
public int getId() {
-
return id;
-
}
-
-
public void setId(int id) {
-
this.id = id;
-
}
-
}
-
-
The Result is :
-
-
[junit] ------------- Standard Error -----------------
-
[junit] {2=com.Child@6c585a, 1=com.Child@11ca803}
-
[junit] key class = class java.lang.String
-
[junit] key class = class java.lang.String
-
[junit] ------------- ---------------- ---------------
按理说作为 Map 的 key 应该是 java.lang.Integer,但是现在打出来的却是 java.lang.String,不知道是什么问题。已经看过[它的 HOWTO],但它会多加一层标签,和我想要的并不一致。由于没时间细看并提供补丁,只好先发了一个 [bug report],等开发者解决或者以后再看。现在只能先把 int 改成 String 先用着再说。




Post new comment