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

浪尽此生 提交于 2019-12-13 05:57:49

问题


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.

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.

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.

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


回答1:


Seems like a typical use case for XPath

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

This tutorial could help:

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/

Here for example is information on this:

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




回答2:


Since you don't know your element name, I would suggest creating a DOM tree and iterating through it. 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.

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.



来源:https://stackoverflow.com/questions/12255529/how-to-extract-xml-tag-value-without-using-the-tag-name-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!