我有xml,看起来有点类似于以下内容。我需要从属性和标签获取值并存储它们。但我不明白如何到达2级(3,4等)嵌套元素。我使用以下代码,我发现在互联网上,它使用DOM但我不能得到正确的RegNumber及其属性。提前致谢。Java:阅读许多嵌套元素的XML
nodeList = xmldocument.getElementsByTagName("Header"); if (nodeList != null && nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { //get the header element Element header = (Element) nodeList.item(i); System.out.println("Element: " + ((Element)nodeList.item(i)).getNodeName()); System.out.println(header.getAttribute("time")); } } nodeList = xmldocument.getElementsByTagName("Document"); if (nodeList != null && nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { //get the document element Element document = (Element) nodeList.item(i); System.out.println("Element: " + ((Element)nodeList.item(i)).getNodeName()); System.out.println(document.getAttribute("Id")); nodeList = document.getElementsByTagName("RegNumber"); for (int j = 0; j < nodeList.getLength(); j++) { //get the RegNumber element Element regNumber = (Element) nodeList.item(j); System.out.println("Element: " + ((Element)nodeList.item(j)).getNodeName()); System.out.println(regNumber.getAttribute("regpoint")); } } }
XML:
<XML xsi:schemaLocation="http://www.codetools.it XSD2.xsd" xmlns="http://www.codetools.it" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header time="2001-12-17T09:30:47Z" />
<Document Id="456">
<RegNumber regpoint="" regdate="2001-12-17T09:30:47Z">123/456</RegNumber>
<Confident flag="0"/>
<DocNumber kind="">
<RegNumber RegPoint="" regdate="2001-12-17T09:30:47Z">456/789</RegNumber>
<Organization fullname="lol" shortname="" ownership="lol" ogrn="78945612" inn="">
<OfficialPerson>
<Name Firstname="John"/>
<Official Department="" post=""/>
<SignDate>2001-12-17T09:30:47Z</SignDate>
</OfficialPerson>
<Econtact type="1">[email protected]</Econtact>
<Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/>
</Organization>
</DocNumber>
<DocTransfer os="Windows" type=".docx" type_ver="" char_set="" description="kkkk">
</DocTransfer>
<Reghistory idnumber="">
<RegNumber RegPoint="" regdate=""/>
<Organization fullname="" shortname="" ownership="" ogrn="" inn="">
<Econtact type=""/>
<Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/>
</Organization>
</Reghistory>
<Author>
<Organization fullname="" shortname="" ownership="" ogrn="12345678" inn="">
<OfficialPerson>
<Name Firstname="" />
<Official Department="" post=""/>
<SignDate>2011-12-17T09:30:47Z</SignDate>
</OfficialPerson>
<Econtact type="Рї">[email protected]</Econtact>
<Address street="" postcode="1234" postbox="" flat="" district="" region="" country="" nontypical="" house="12"/>
</Organization>
<PrivatePerson>
<Name Surname="" Firstname="" Fathername=""/>
<Econtact type=""/>
<Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/>
<SignDate/>
<Rank privilege="" socialposition="" sex=""/>
</PrivatePerson>
</Author>
<Validator attestation="0">
<DocNumber kind="наказ">
<RegNumber RegPoint="" regdate="2001-12-17T09:30:47Z"/>
<Organization fullname="" shortname="" ownership="" ogrn="14725836" inn="">
<Econtact type="Рї">[email protected]</Econtact>
<Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/>
</Organization>
</DocNumber>
<Organization fullname="" shortname="РљРњРЈ" ownership="" ogrn="96325878" inn="">
<Econtact type="String">[email protected]</Econtact>
<Address street="Пушкіна" settlement="" postcode="4563" postbox="" flat="" district="" region="" country="" nontypical="" house="23"/>
<OfficialPerson>
<Name Surname="" Firstname="" Fathername=""/>
<Official Department="" post=""/>
<SignDate>2011-12-17T09:30:47Z</SignDate>
</OfficialPerson>
</Organization>
<PrivatePerson>
<Name Surname="" Firstname="" Fathername=""/>
<Econtact type=""></Econtact>
<Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/>
<SignDate></SignDate>
<Rank privilege="" socialposition="" sex=""/>
</PrivatePerson>
</Validator>
<Addressee>
<Referred id="" retype="">
<RegNumber regdate="" regpoint=""/>
<TaskNumber taskDate=""/>
</Referred>
<Organization fullname="String" shortname="" ownership="" ogrn="85236974" inn="">
<OfficialPerson>
<Name Surname="" Firstname="" Fathername=""/>
<Official Department="" post=""/>
</OfficialPerson>
<Econtact type="Рї">[email protected]</Econtact>
<Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" type="" house=""/>
</Organization>
<PrivatePerson>
<Name Surname="" Firstname="" Fathername=""/>
<Econtact type=""></Econtact>
<Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/>
<Rank privilege="" socialposition="" sex=""/>
</PrivatePerson>
</Addressee>
<Writer>
<Organization fullname="" shortname="" ownership="" ogrn="" inn="">
<OfficialPerson>
<Name Surname="" Firstname="" Fathername=""/>
<Econtact type=""></Econtact>
</OfficialPerson>
</Organization>
<PrivatePerson>
<Name Surname="" Firstname="" Fathername=""/>
<Econtact type=""></Econtact>
</PrivatePerson>
</Writer>
</Document>
编辑。据建议,我试图弄清楚JAXB thingy。 xjc工具生成了3个类:ObjectFactory,package-info,XML。我发现这个代码从XML创建对象:
public static void main(String[] args) {
try {
File file = new File("F:\\Untitled3.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(XML.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
XML xml = (XML) jaxbUnmarshaller.unmarshal(file);
System.out.println(xml);
} catch (JAXBException e) {
e.printStackTrace();
}
}
这是正确的方法,或者我需要使用的ObjectFactory?:
ObjectFactory f =new ObjectFactory();
XML xml1 = f.createXML();
如何创建对象所有的标签和属性,然后从它创建xml文件?我新来java和jaxb的东西,所以请忍受我。
你真的想手动解析XML吗?有更好的方法,看看[JAXB](http://en.wikipedia.org/wiki/Java_Architecture_for_XML_Binding) –
谢谢你的回答。我真的不知道我应该用什么。我刚刚通过Google搜索“Java xml read”发现了Dom的东西,它对我来说很简单。您能否给我一些关于JAXB初学者的链接? – bunnyjesse112