<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="XXXXXXX">
<cuobject type-id="EmailSubscription" object-id="[email protected]">
<object-attribute attribute-id="exported">true</object-attribute>
<object-attribute attribute-id="firstName">jjj</object-attribute>
<object-attribute attribute-id="lastName">jjj</object-attribute>
<object-attribute attribute-id="subscribed">true</object-attribute>
</cuobject>
<cuobject type-id="EmailSubscription" object-id="[email protected]">
<object-attribute attribute-id="exported">true</object-attribute>
<object-attribute attribute-id="firstName">ghh</object-attribute>
<object-attribute attribute-id="lastName">fhh</object-attribute>
<object-attribute attribute-id="subscribed">true</object-attribute>
</cuobject>
<cuobject type-id="EmailSubscription" object-id="[email protected]">
<object-attribute attribute-id="exported">true</object-attribute>
<object-attribute attribute-id="firstName">mmm</object-attribute>
<object-attribute attribute-id="lastName">mmm</object-attribute>
<object-attribute attribute-id="subscribed">true</object-attribute>
</cuobject>
</objects>
How to read this xml file in java (need to be done by using exported,firstName,lastName,...)?
how do we do that?
I am doing it like
NodeList nList = doc.getElementsByTagName("cuobject");
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node nNode = nList.item(temp);
System.out.println("
Current Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) nNode;
String email = eElement.getAttribute("object-id");
String firstName = eElement.
getElementsByTagName("object-attribute").item(1).getTextContent();
String lastName = eElement.
getElementsByTagName("object-attribute").item(2).getTextContent();
String subscribed = eElement.
getElementsByTagName("object-attribute").item(3).getTextContent();
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…