[英]Java: reading xml with many nested elements
我有看起來與以下內容類似的xml。 我需要從屬性和標簽中獲取值並存儲它們。 但是我不明白如何到達2(3,4等)嵌套元素。 我使用以下代碼在Internet上找到,它使用DOM,但是我無法獲取正確的RegNumber及其屬性。 提前致謝。
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">test@test.com.ua</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="Рї">smirnov@test.com</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="Рї">kmu@test.com</Econtact>
<Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/>
</Organization>
</DocNumber>
<Organization fullname="" shortname="РљРњРЈ" ownership="" ogrn="96325878" inn="">
<Econtact type="String">kmu@test.com</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="Рї">sofki@test.com</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的問題。 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的新手,請耐心等待。
它對於讓您訪問特定深度的節點的方法通常沒有意義或通常沒有用。 這不僅與Java有關,而且與DOM規范有關,我相信這是正確的決定。
人們通常通過許多有意義的方式之一來訪問節點:
查找xpath,這將非常有幫助。
在每個節點上再次使用“ getElementsByTagName”功能,以按標簽名稱獲取元素。 明顯...
這是我的示例代碼,深入了幾個層次:
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(acpFile);
Element docEle = doc.getDocumentElement();
NodeList series = docEle.getElementsByTagName("dod:recordSeries");
if(series != null && series.getLength()>0)
{
for(int i=0; i<series.getLength(); i++)
{
Element serie = (Element)series.item(i);
System.out.println("S -- "+serie.getAttribute("view:childName"));
NodeList categories = serie.getElementsByTagName("dod:recordCategory");
for(int j=0; j<categories.getLength(); j++)
{
Element category = (Element)categories.item(j);
System.out.println("C ---- "+category.getAttribute("view:childName"));
NodeList recordFolders = category.getElementsByTagName("rma:recordFolder");
for(int k=0; k<recordFolders.getLength(); k++)
{
Element folder = (Element)recordFolders.item(k);
System.out.println("F ------ "+folder.getAttribute("view:childName"));
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.