5

In an xml file, how do I get the data in a label according to another label in t...

 3 years ago
source link: https://www.codesd.com/item/in-an-xml-file-how-do-i-get-the-data-in-a-label-according-to-another-label-in-the-same-node-level.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

In an xml file, how do I get the data in a label according to another label in the same node level?

advertisements

I have a xml file like the following:

<root>
<doc>
  <str name="name">test1</str>
  <str name="uri">test1.com</str>
</doc>
<doc>
  <str name="name">test2</str>
  <str name="uri">test1.com</str>
</doc>
 </root>

I want to parsing the file. I could get the name using the dom4j.

for(Element doc : docs ){
          List<Element> strs = doc.elements();
          for(Element str : strs ){
              if("name".equals(str.attributeValue("name"))){
                 System.out.println(str.getText());
                    }
                }
            }

How can I get the data of 'uri' after I getting the data of 'name'under the same node 'doc'?

Thaks a lot!


Isn't enough the following:

for(Element doc : docs ){
   List<Element> strs = doc.elements();
   for(Element str : strs ){
      if("name".equals(str.attributeValue("name"))){
         System.out.println(str.getText());
      }
      if("uri".equals(str.attributeValue("name"))){
         System.out.println(str.getText());
      }
   }
}

as you're looping through the child of "doc"


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK