[英]Reading information from XML file using Java
I have the following XML file generated by some tool.我有一些工具生成的以下 XML 文件。 I have to convert them into java objects.我必须将它们转换成 java 个对象。 It looks similar to this它看起来与此类似
<A>
<B name = "ming">
<B name = "giang">
<B name = "long">
<text>CCVVV</text>
</B>
<B name = "long4">
<text>CCVVvvvV</text>
</B>
</B>
</B>
</A>
How do I extract those information from that xml using java so I can get the smallest child in that XML file(<B name = "long"> and <B name = "long4">).我如何使用 java 从 xml 中提取这些信息,以便我可以获得该 XML 文件中最小的子文件(<B name = "long"> 和 <B name = "long4">)。 I am thinking of creating classes like this我正在考虑创建这样的类
class B{
List<B> objects;
String text;
}
class A{
B objectB;
}
Generate XSD by your XML ( https://www.google.com/search?q=xsd+by+xml )通过您的 XML ( https://www.google.com/search?q=xsd+by+xml ) 生成 XSD
Generate java classes by XSD using xjc tool (jdk\bin\xjc.exe classa.xsd -verbose -catalog C:\tmp\classa)使用xjc工具(jdk\bin\xjc.exe classa.xsd -verbose -catalog C:\tmp\classa)通过XSD生成java个类
Easily unmarshal objects from xml like this:像这样轻松解组 xml 中的对象:
JAXBContext context; context = JAXBContext.newInstance(AClass.class); Unmarshaller u = context.createUnmarshaller(); AClass obj = (AClass) u.unmarshal(new File("object.xml"));
UPD: src\generated\A.java UPD: src\generated\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: 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: 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"));
...
}
}
You can use the Java DOM API. Here is an example how you can create and read a XML file.您可以使用 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
Now you can recursively iterate through the child elements and nodes and build your Java object structure.现在您可以递归地遍历子元素和节点并构建您的 Java object 结构。 I have done something similar in a more general project here for example: https://github.com/stonar96/Configuration/blob/master/src/main/java/com/vanillage/utils/configuration/xml/XmlConfigurationParser.java#L63我在这里的一个更一般的项目中做了类似的事情,例如: https://github.com/stonar96/Configuration/blob/master/src/main/java/com/vanillage/utils/configuration/xml/XmlConfigurationParser.java# L63
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.