如何在不使用Java中标记名的情况下提取xml标记值? [英] How to extract xml tag value without using the tag name in java?

查看:83
本文介绍了如何在不使用Java中标记名的情况下提取xml标记值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用java.我有一个看起来像这样的xml文件:

I am using java.I have an xml file which looks like this:

<?xml version="1.0"?>
      <personaldetails>
          <phno>1553294232</phno>
          <email>
                <official>xya@gmail.com</official>
                <personal>bk@yahoo.com</personal>
          </email>
      </personaldetails>

现在,我需要使用特定条件检查每个标记值的类型,并将它们放在单独的文件中.

Now,I need to check each of the tag values for its type using specific conditions,and put them in separate files.

例如,在上面的文件中,我写的条件是10位数等于电话号码, 格式为xxx@yy.com的电子邮件是电子邮件.

For example,in the above file,i write conditions like 10 digits equals phone number, something in the format of xxx@yy.com is an email..

因此,我需要做的是提取每个标签中的标签值,如果它符合特定条件,则将其放在第一个文本文件中,如果不包含在第二个文本文件中. 在这种情况下,第一个文本文件将包含:

So,what i need to do is i need to extract the tag values in each tag and if it matches a certain condition,it is put in the first text file,if not in the second text file. in that case,the first text file will contain:

1553294232
xya@gmail.com
bk@yahoo.com

和第二个文件中的其余值.

and the rest of the values in the second file.

我只是不知道如何在不使用标签名称的情况下(或在不使用GetElementsByTagName的情况下)提取标签值. 我的意思是即使我给出< mailing>而不是<personal>标签,该代码也应提取电子邮件bk@yahoo.com.它不取决于标签名称.

i just don't know how to extract the tag values without using the tag name.(or without using GetElementsByTagName). i mean this code should extract the email bk@yahoo.com even if i give <mailing> instead of <personal> tag.It should not depend on the tag name.

希望我不会感到困惑.我是使用xml的Java新手.所以,如果我的问题很傻,请原谅我. 请帮忙.

Hope i am not confusing.I am new to java using xml.So,pardon me if my question is silly. Please Help.

推荐答案

似乎是 XPath的典型用例

XPath允许您以非常灵活的方式查询XML.

XPath allows you to query XML in a very flexible way.

本教程可以帮助您

http://www.javabeat.net /2009/03/how-to-query-xml-using-xpath/

如果您使用的是Java script (可能是这种情况),因为提到了getElementsByTagName(),则可以只使用JQuery选择器,它将为您提供跨浏览器的一致行为,并且如果您尚未使用JQuery库,它还可以用于许多其他事情... http://api.jquery.com/category/selectors/

If you're using Java script, which could to be the case, since you mention getElementsByTagName(), you could just use JQuery selectors, it will give you a consistent behavior across browsers, and JQuery library is useful for a lot of other things, if you are not using it already... http://api.jquery.com/category/selectors/

例如,有关此的信息:

http://www.switchonthecode.com/tutorials/xml-parsing- with-jquery

这篇关于如何在不使用Java中标记名的情况下提取xml标记值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆