简体   繁体   English

使用SAX Parser解析XML时需要获取element标签的值

[英]Need to get the value of the element tag when parsing XML with SAX Parser

Grabbing an XML file off of the internet and feeding it's data into a database. 从互联网上获取XML文件,并将其数据输入数据库。 Most of it works just fine. 大部分都能正常工作。 But I have a problem with xml tags having the same name but different values attached. 但是我对具有相同名称但附加了不同值的xml标签存在问题。

So, we have an xml file like this: 因此,我们有一个这样的xml文件:

 <Overtag> <Tag> Name </Tag> <SubTag> TextSubTag </SubTag> <TagWithValue value="SomeValue"> TextTagWithValue </TagWithValue> </Overtag> <Overtag>... 

I can set up a NodeList by Overtag. 我可以通过Overtag设置NodeList。 I can get a nodeist of Overtag's Children which I call Children. 我可以得到Overtag的Children(我称为Children)的节点主义者。

So, I run this over a for loop - for(int nN=0; nN 因此,我在for循环上运行此代码-for(int nN = 0; nN

I grab the Text of the Tags themselves: 我自己抓取标签的文本:

String sTag = Children.item(nN).getNodeName(); 字符串sTag = Children.item(nN).getNodeName();

I can even grab the text between the Tags: Children.item(nN).getTextContent() 我什至可以抓取Tag之间的文本:Children.item(nN).getTextContent()

BUT I need to organize this text based on the value. 但是我需要根据值来组织此文本。

What command can I use to get "SomeValue" if nN = the childlist number for (in this case 2)? 如果nN =子列表号(在这种情况下为2),我可以使用什么命令来获取“ SomeValue”?

As in: Children.item(nN).? 如:Children.item(nN)。

Found it... 找到了...

if using NodeList to list out your xml tags, you need to return it to an element, then use getAttribute; 如果使用NodeList列出您的xml标记,则需要将其返回到元素,然后使用getAttribute; so if the tag is (assuming your list with TagWithValue in it is a NodeList called Children): 因此,如果标签是(假设您的列表中包含TagWithValue的是一个名为Children的NodeList):

Element eChild = (Element) Children.item(nN);
                String sAtt = eChild.getAttribute("value");`

This will give you sAtt = "SomeValue". 这将为您提供sAtt =“ SomeValue”。 Sorry to waste space by posting then finding the answer two hours later; 很抱歉浪费空间,因为在发布后两个小时后找到了答案; hopefully someone else finds this useful. 希望其他人觉得这很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM