使用具有多个节点的特定子进程读取XML文件

时间:2013-11-27 05:41:00

标签: java xml

try {
File fXmlFile = new File("C:/Users/priran/Desktop/vendavo20131118030948370.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-
//parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

NodeList nList = doc.getElementsByTagName("VUser");

System.out.println("----------------------------");

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

System.out.println("\nCurrent Element :" + nNode.getNodeName());

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;



System.out.println(eElement.getElementsByTagName("VName").item(0)
    .getTextContent());
System.out.println(eElement.getElementsByTagName("VPassword").item(0)
.getTextContent());
System.out.println(eElement.getElementsByTagName("VFullName").item(0)
.getTextContent());
System.out.println(eElement.getElementsByTagName("VValidity").item(0).
getTextContent());
System.out.println(eElement.getElementsByTagName("VDefaultUOM").item(0)
.getTextContent());
System.out.println(eElement.getElementsByTagName("VDefaultCurrency").item(0)
.getTextContent());
System.out.println(eElement.getElementsByTagName("VGroups").item(0).
getTextContent());
System.out.println(eElement.getElementsByTagName("VRoles").item(0).
getTextContent());
System.out.println(eElement.getElementsByTagName("VIsSysAdmin").item(0).
getTextContent());
System.out.println(eElement.getElementsByTagName("VUIProfile").item(0).
getTextContent());
System.out.println(eElement.getElementsByTagName("VTimeZone").item(0).
getTextContent());
System.out.println(eElement.getElementsByTagName("VDefaultLocale").item(0).
getTextContent());


}
}
} catch (Exception e) {
e.printStackTrace();
}

我尝试阅读以下XML

 
- <entity-data>
- <VUser>
  <VName>Name</VName> 
  <VPassword>Password</VPassword> 
  <VFullName>FullName</VFullName> 
  <VValidity>Validity</VValidity> 
  <VDefaultUOM>DefaultUOM</VDefaultUOM> 
  <VDefaultCurrency>DefaultCurrency</VDefaultCurrency> 
- <VGroups>
  <VGroup>Groups</VGroup> 
  </VGroups>
- <VRoles>
  <VRole>Roles</VRole> 
  </VRoles>
  <VIsSysAdmin>IsSysAdmin</VIsSysAdmin> 
  <VUIProfile>UIProfile</VUIProfile> 
  <VTimeZone>TimeZone</VTimeZone> 
  <VDefaultLocale>DefaultLocale</VDefaultLocale> 
  </VUser>
- <VUser>
  <VName>MCLENDJ</VName> 
  <VPassword>abcdefg1</VPassword> 
  <VFullName>Jack McLendon</VFullName> 
  <VValidity>1/1/2001..eot</VValidity> 
  <VDefaultUOM>Base</VDefaultUOM> 
  <VDefaultCurrency>USD</VDefaultCurrency> 
- <VGroups>
  <VGroup>AllCountrys</VGroup> 
  <VGroup>AllProducts</VGroup> 
  </VGroups>
- <VRoles>
  <VRole>BusinessAnalyst</VRole> 
  </VRoles>
  <VIsSysAdmin>FALSE</VIsSysAdmin> 
  <VUIProfile>BusinessAnalyst</VUIProfile> 
  <VTimeZone>US/Eastern</VTimeZone> 
  <VDefaultLocale>en_US</VDefaultLocale> 
  </VUser>
- <VUser>
  <VName>DUVALLM</VName> 
  <VPassword>abcdefg1</VPassword> 
  <VFullName>Mark Duvall</VFullName> 
  <VValidity>1/1/2001..eot</VValidity> 
  <VDefaultUOM>Base</VDefaultUOM> 
  <VDefaultCurrency>USD</VDefaultCurrency> 
- <VGroups>
  <VGroup>US</VGroup> 
  <VGroup>CA</VGroup> 
  <VGroup>2610</VGroup> 
  </VGroups>
- <VRoles>
  <VRole>BusinessAnalyst</VRole> 
  </VRoles>
  <VIsSysAdmin>FALSE</VIsSysAdmin> 
  <VUIProfile>BusinessAnalyst</VUIProfile> 
  <VTimeZone>US/Eastern</VTimeZone> 
  <VDefaultLocale>en_US</VDefaultLocale> 
  </VUser>
  </entity-data>

并且我无法在VGroups Tag中单独读取值为US CA 2610的VGroup标记。我能够读取标记,但是当标记具有子元素时,我无法读取它。 请帮助。

1 个答案:

答案 0 :(得分:0)

在打印之前,如果子计数大于1,则检查元素child,然后获取内部文本内容并将其保存在最后连接al并打印