[英]Reading XML tags getting value from inner tag
I don't know how to explain my situation, I can provide example below. 我不知道如何解释我的情况,我可以在下面提供示例。 I have an XML file to be read in Java, something like this: 我有一个要用Java读取的XML文件,如下所示:
<Author AffiliationIDS="Aff1">
<AuthorName DisplayOrder="Western">
<GivenName>Wei</GivenName>
<GivenName>Long</GivenName>
<FamilyName>
<Value>Tan</Value>
</FamilyName>
</AuthorName>
</Author>
As you can see the <FamilyName>
tag, inside the <FamilyName>
tag is surrounded by a Value tag. 如您所见, <FamilyName>
标记在<FamilyName>
标记内被Value标记包围。 This is because in the xsd it stated the element as maxOccurs="unbounded"
which mean more than one value can be in that element tag. 这是因为在xsd中它将元素表示为maxOccurs="unbounded"
,这意味着该元素标记中可以包含多个值。 How should I modify the code so that it can read in the <FamilyName>
tag and get Value
tag element no matter how many occurrence of the Value
exist? 我应该如何修改代码,以便它可以在读<FamilyName>
标签和获得Value
标签元素无论许多发生怎样Value
存在吗?
Example: 例:
<FamilyName>
<Value>Sarah</Value>
<Value>Johnson</Value>
</FamilyName>
The code look like this. 代码看起来像这样。
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File fXmlFile = new File("/fileaddress/test-1.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("AuthorName");
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("Given Name : " + eElement.getElementsByTagName("GivenName").item(0).getTextContent());
System.out.println("Family Name : " + eElement.getElementsByTagName("FamilyName").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Get the FamilyName node by getElementsByTagName("FamilyName").item(0) and loop over its child nodes (.getChildNodes()) and get the value of the textNode 通过getElementsByTagName(“ FamilyName”)。item(0)获取FamilyName节点,并遍历其子节点(.getChildNodes())并获取textNode的值
Or, 要么,
You can even getElementsByTagName("Value") if you are sure that value tag does not occur anywhere else other than inside FamilyName 如果您确定value标记除了FamilyName内的其他地方都没有出现,您甚至可以getElementsByTagName(“ Value”)
Here is a code Sample 这是一个代码示例
NodeList children = doc.getElementsByTagName("FamilyName").item(0).getChildNodes();
for(int i=0;i<children.getLength();i++) {
if(children.item(i).getNodeType()== Node.ELEMENT_NODE) {
Element child = (Element)children.item(i);
System.out.println(child.getTextContent());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.