我试图整理和解组XML。为简单起见,我删除了名称空间的任何注释。
以下是我最终寻找的XML结构:
x = (width*25.4)/(dpi);
我是RootElement类,如下所示:
<?xml version="1.0" encoding="utf-16"?>
<RootElement>
<Parent1>
<Parent1Child1>
<Parent1Child1Child1>v1</Parent1Child1Child1>
<Parent1Child1Child2>v2</Parent1Child1Child2>
<Parent1Child1Child3>v3</Parent1Child1Child3>
</Parent1Child1>
<Parent1Child2>v4</Parent1Child2>
</Parent1>
</RootElement>
我是'Parent1Child1&#39;课程如下:
package com.mysoftkey.jaxb;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
//@XmlRootElement(name="RootElement",namespace="http://yyyy")
@XmlRootElement(name="RootElement")
public class RootElement {
//@XmlElementWrapper(name="Parent1",namespace="http://hhhh")
@XmlElementWrapper(name="Parent1")
@XmlElement(name="Parent1Child1")
private ArrayList<Parent1Child1> p1;
private String Parent1Child2;
public void setparent1child1(ArrayList<Parent1Child1> Parent1) {
this.p1=Parent1;
}
public ArrayList<Parent1Child1> getparent1child1(){
return p1;
}
public String getparent1child2() {
return Parent1Child2;
}
public void setparent1child2(String Parent1Child2) {
this.Parent1Child2=Parent1Child2;
}
public void addparent1child1(Parent1Child1 Parent1Child1) {
try {
if (p1==null) {
p1=new ArrayList<Parent1Child1>();
}
p1.add(Parent1Child1);
}catch (Exception e) {
}
}
}
我是编组/解组的主要课程,如下所示:
package com.mysoftkey.jaxb;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Parent1Child1")
public class Parent1Child1 {
private String Parent1Child1Child1;
private String Parent1Child1Child2;
private String Parent1Child1Child3;
public String getparent1child1child1() {
return Parent1Child1Child1;
}
public void setparent1child1child1(String Parent1Child1Child1) {
this.Parent1Child1Child1=Parent1Child1Child1;
}
public String getparent1child1child2() {
return Parent1Child1Child2;
}
public void setparent1child1child2(String Parent1Child1Child2) {
this.Parent1Child1Child2=Parent1Child1Child2;
}
public String getparent1child1child3() {
return Parent1Child1Child3;
}
public void setparent1child1child3(String Parent1Child1Child3) {
this.Parent1Child1Child3=Parent1Child1Child3;
}
}
当我执行上述操作时,我得到以下内容:
package com.mysoftkey.jaxb;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class ParseXML1 {
public static String xml_filepath="C:\\...\\Testxml2.xml";
public static void main(String[] args) throws JAXBException, FileNotFoundException {
// TODO Auto-generated method stub
//Create Parent1Child1 object and populate some values
Parent1Child1 pcd1=new Parent1Child1();
pcd1.setparent1child1child1("value1");
pcd1.setparent1child1child2("value2");
pcd1.setparent1child1child3("value3");
RootElement rt1=new RootElement();
rt1.addparent1child1(pcd1);
rt1.setparent1child2("value4");
//Initiate marshaller class
JAXBContext cont=JAXBContext.newInstance(RootElement.class);
Marshaller m=cont.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(rt1, System.out);
m.marshal(rt1, new File("C:\\...\\Testxml3.xml"));
//Create JAXB context and initiate unmarshaller
Unmarshaller um=cont.createUnmarshaller();
RootElement root1=(RootElement)um.unmarshal(new FileReader("C:\\...\\Testxml3.xml"));
ArrayList<Parent1Child1> pc1=root1.getparent1child1();
//System.out.println(pc1.size());
for (Parent1Child1 p:pc1)
{
System.out.println(p.getparent1child1child1());
System.out.println(p.getparent1child1child2());
System.out.println(p.getparent1child1child3());
}
System.out.println(root1.getparent1child2());
}
}
有人可以让我知道为什么&#39; Parent1Child1&#39;标签重复两次。进入“父母1”之后包装,再次在它外面。
如果我评论&#39; RootElement&#39;的下一部分。我得到了父母1孩子&#39;只标记一次。我不应该在“父亲1孩子”中注释&#39; parent1child1&#39;作为XMLElement?请帮助理解。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RootElement>
<Parent1>
<Parent1Child1>
<parent1child1child1>value1</parent1child1child1>
<parent1child1child2>value2</parent1child1child2>
<parent1child1child3>value3</parent1child1child3>
</Parent1Child1>
</Parent1>
<parent1child1>
<parent1child1child1>value1</parent1child1child1>
<parent1child1child2>value2</parent1child1child2>
<parent1child1child3>value3</parent1child1child3>
</parent1child1>
<parent1child2>value4</parent1child2>
</RootElement>
value1
value2
value3
value4
在评论&#39; RootElement&#39;中的上述行后输出类:
@XmlElementWrapper(name="Parent1")
@XmlElement(name="parent1child1")
由于