问题描述
我有一些工具生成的以下XML文件。我必须将它们转换为Java对象。看起来与此类似
<A>
<B name = "ming">
<B name = "giang">
<B name = "long">
<text>CCVVV</text>
</B>
<B name = "long4">
<text>CCVVvvvV</text>
</B>
</B>
</B>
</A>
如何使用Java从该xml中提取这些信息,以便获得该XML文件中的最小子项(和)。我正在考虑创建这样的类
class B{
List<B> objects;
String text;
}
class A{
B objectB;
}
解决方法
-
通过XML(https://www.google.com/search?q=xsd+by+xml)生成XSD
-
使用xjc工具(jdk \ bin \ xjc.exe classa.xsd -verbose -catalog C:\ tmp \ classa)通过XSD生成Java类
-
像这样从xml轻松解组对象:
JAXBContext context; context = JAXBContext.newInstance(AClass.class); Unmarshaller u = context.createUnmarshaller(); AClass obj = (AClass) u.unmarshal(new File("object.xml"));
UPD: src \ generation \ A.java
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation,v2.2.4
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2020.09.01 at 04:57:38 PM MSK
//
package generated;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="B">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="B">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="B" maxOccurs="unbounded">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="text" type="{http://www.w3.org/2001/XMLSchema}string"/>
* </sequence>
* <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "",propOrder = {
"b"
})
@XmlRootElement(name = "A")
public class A {
@XmlElement(name = "B",required = true)
protected A.B b;
/**
* Gets the value of the b property.
*
* @return
* possible object is
* {@link A.B }
*
*/
public A.B getB() {
return b;
}
/**
* Sets the value of the b property.
*
* @param value
* allowed object is
* {@link A.B }
*
*/
public void setB(A.B value) {
this.b = value;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="B">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="B" maxOccurs="unbounded">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="text" type="{http://www.w3.org/2001/XMLSchema}string"/>
* </sequence>
* <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "",propOrder = {
"b"
})
public static class B {
@XmlElement(name = "B",required = true)
protected A.B.B1 b;
@XmlAttribute(name = "name",required = true)
protected String name;
/**
* Gets the value of the b property.
*
* @return
* possible object is
* {@link A.B.B1 }
*
*/
public A.B.B1 getB() {
return b;
}
/**
* Sets the value of the b property.
*
* @param value
* allowed object is
* {@link A.B.B1 }
*
*/
public void setB(A.B.B1 value) {
this.b = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="B" maxOccurs="unbounded">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="text" type="{http://www.w3.org/2001/XMLSchema}string"/>
* </sequence>
* <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "",propOrder = {
"b"
})
public static class B1 {
@XmlElement(name = "B",required = true)
protected List<A.B.B1.B2> b;
@XmlAttribute(name = "name",required = true)
protected String name;
/**
* Gets the value of the b property.
*
* <p>
* This accessor method returns a reference to the live list,* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the b property.
*
* <p>
* For example,to add a new item,do as follows:
* <pre>
* getB().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link A.B.B1.B2 }
*
*
*/
public List<A.B.B1.B2> getB() {
if (b == null) {
b = new ArrayList<A.B.B1.B2>();
}
return this.b;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="text" type="{http://www.w3.org/2001/XMLSchema}string"/>
* </sequence>
* <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "",propOrder = {
"text"
})
public static class B2 {
@XmlElement(required = true)
protected String text;
@XmlAttribute(name = "name",required = true)
protected String name;
/**
* Gets the value of the text property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getText() {
return text;
}
/**
* Sets the value of the text property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setText(String value) {
this.text = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
}
}
}
}
src \ Generated \ ObjectFactory.java:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation,v2.2.4
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2020.09.01 at 04:57:38 PM MSK
//
package generated;
import javax.xml.bind.annotation.XmlRegistry;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the generated package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions,element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link A }
*
*/
public A createA() {
return new A();
}
/**
* Create an instance of {@link A.B }
*
*/
public A.B createAB() {
return new A.B();
}
/**
* Create an instance of {@link A.B.B1 }
*
*/
public A.B.B1 createABB() {
return new A.B.B1();
}
/**
* Create an instance of {@link A.B.B1.B2 }
*
*/
public A.B.B1.B2 createABBB() {
return new A.B.B1.B2();
}
}
src \ XMLTest.java:
import generated.A;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;
public class XMLTest {
public static void main(String[] args) throws Exception {
JAXBContext context;
context = JAXBContext.newInstance(A.class);
Unmarshaller u = context.createUnmarshaller();
A obj = (A) u.unmarshal(new File("object.xml"));
...
}
}
,
您可以使用Java DOM API。这是一个如何创建和读取XML文件的示例。
String string = "text";
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element element = document.createElement("element");
element.appendChild(document.createTextNode(string));
document.appendChild(element);
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document),new StreamResult(new File("test.xml")));
// creates <?xml version="1.0" encoding="UTF-8" standalone="no"?><element>text</element>
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("test.xml"));
System.out.println(document.getDocumentElement().getTextContent()));
// prints text
现在,您可以递归地遍历子元素和节点并构建Java对象结构。例如,我在一个更一般的项目中做了类似的事情:https://github.com/stonar96/Configuration/blob/master/src/main/java/com/vanillage/utils/configuration/xml/XmlConfigurationParser.java#L63