简体   繁体   English

如何在不使用Java中标签名称的情况下提取xml标签值?

[英]How to extract xml tag value without using the tag name in java?

I am using java.I have an xml file which looks like this: 我正在使用java。我有一个看起来像这样的xml文件:

<?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. 现在,我需要使用特定条件检查每个标记值的类型,并将它们放在单独的文件中。

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.. 例如,在上面的文件中,我写的条件是10位数等于电话号码,格式为xxx@yy.com的是电子邮件。

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. 其余的值在第二个文件中。

i just don't know how to extract the tag values without using the tag name.(or without using GetElementsByTagName). 我只是不知道如何在不使用标签名称的情况下(或在不使用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. 我的意思是即使我给出< mailing>而不是<personal>标签,此代码也应提取电子邮件bk@yahoo.com。它不取决于标签名称。

Hope i am not confusing.I am new to java using xml.So,pardon me if my question is silly. 希望我不要混淆。我是使用xml的Java新手。所以,请问我的问题很傻。 Please Help. 请帮忙。

Seems like a typical use case for XPath 似乎是XPath的典型用例

XPath allows you to query XML in a very flexible way. XPath允许您以非常灵活的方式查询XML。

This tutorial could help: 本教程可以帮助您:

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

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/ 如果您使用的是Java 脚本 (可能是这种情况),因为提到了getElementsByTagName(),则可以只使用JQuery选择器,它将为您提供跨浏览器的一致行为,并且JQuery库对于许多其他事情很有用,如果您尚未使用它的话... http://api.jquery.com/category/selectors/

Here for example is information on this: 例如,以下是有关此信息:

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

Since you don't know your element name, I would suggest creating a DOM tree and iterating through it. 由于您不知道element名称,因此建议您创建一个DOM树并对其进行遍历。 As and when you get a element , you would try to match it against your ruleset ( and I would suggest using regex for this purpose ) and then write it to your a file. 当您获得一个element ,您将尝试将其与您的ruleset进行匹配( 为此,我建议使用regex ),然后将其写入文件中。

This would be a sample structure to help you get started, but you would need to modify it based on your requirement: 这将是一个示例结构,可以帮助您入门,但是您需要根据需要进行修改:

public void parseXML(){
    try{
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc; 

        doc = documentBuilder.parse(new File("test.xml"));
        getData(null, doc.getDocumentElement());
    }catch(Exception exe){
        exe.printStackTrace();
    }
}

private void getData(Node parentNode, Node node){

    switch(node.getNodeType()){
        case Node.ELEMENT_NODE:{

            if(node.hasChildNodes()){
                NodeList list = node.getChildNodes();
                int size = list.getLength();

                for(int index = 0; index < size; index++){
                    getData(node, list.item(index));
                }
            }

            break;
        }

        case Node.TEXT_NODE:{
            String data = node.getNodeValue();

            if(data.trim().length() > 0){
                /*
                 * Here you need to check the data against your ruleset and perform your operation
                 */
                System.out.println(parentNode.getNodeName()+" :: "+node.getNodeValue());
            }
            break;
        }

    }
}

You might want to look at the Chain of Responsibility design pattern to design your ruleset. 您可能需要查看Chain of Responsibility设计模式来设计规则集。

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

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