我如何使用Jaxb解析XML。
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.xyz.in"><root><Del><NUMBER>13691991</NUMBER><PIECES>2</PIECES><SHEETNO>D1415/001005</SHEETNO></Del></root></string>
答案 0 :(得分:0)
尝试创建这些类:
Del类
package jaxb;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement( name = "Del")
public class Del {
@XmlElement(name = "NUMBER")
private String number;
@XmlElement(name = "PIECES")
private String pieces;
@XmlElement(name = "SHEETNO")
private String sheetno;
public Del() {
super();
}
public void setNumber(String number) {
this.number = number;
}
public String getNumber() {
return number;
}
public void setPieces(String pieces) {
this.pieces = pieces;
}
public String getPieces() {
return pieces;
}
public void setSheetno(String sheetno) {
this.sheetno = sheetno;
}
public String getSheetno() {
return sheetno;
}
}
MyString类
package jaxb;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement( name = "string")
public class MyString {
@XmlElement(name = "root")
private Root root;
public MyString() {
super();
}
public void setRoot(Root root) {
this.root = root;
}
public Root getRoot() {
return root;
}
}
类Root
package jaxb;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement( name = "root")
public class Root {
@XmlElement(name = "Del")
private Del del;
public Root() {
super();
}
public void setDel(Del del) {
this.del = del;
}
public Del getDel() {
return del;
}
}
文件package-info.java
@XmlSchema( namespace="http://www.xyz.in",
xmlns={ @XmlNs( prefix = "", namespaceURI = "http://www.xyz.in")},
elementFormDefault = XmlNsForm.QUALIFIED)
package jaxb;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
**要测试的主要课程**
package jaxb;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class Main {
public static void main(String[] args){
try {
String file = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<string xmlns=\"http://www.xyz.in\">\n" +
" <root>\n" +
" <Del>\n" +
" <NUMBER>13691991</NUMBER>\n" +
" <PIECES>2</PIECES>\n" +
" <SHEETNO>D1415/001005</SHEETNO>\n" +
" </Del>\n" +
" </root>\n" +
"</string>";
JAXBContext jaxbContext = JAXBContext.newInstance(MyString.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(file);
MyString myString = (MyString)jaxbUnmarshaller.unmarshal(reader);
System.out.println(myString);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}