4

Please help me to put element and text nodes into an array of Strings.

For example an .xml file has:

<soap:Envelope>
  <soap:Body>
    <ser:getTitle>
      <!--Optional:-->
      <ser:title>Meeting</ser:title>
    </ser:getTitle>
    <ser:getDiscription>
      <!--Optional:-->
      <ser:discription>this is the meeting</ser:discription>
    </ser:getDiscription>
    ...
  </soap:Body>
</soap:Envelop>

Now I want to place the values into the String[] key, value as follows:

key[0] = "title";
value[0] = "meeting";
key[1] = "discription";
value[1] = "this is the meeting";

... and so on.

Many thanks in advance!

2
  • Why do you need them in 2 String[]? Why not Map<String, String>? Commented Aug 26, 2012 at 18:45
  • I don't understand what you are trying to do. If you want to extract certain values, than see my answer below. If you want to parse the XML and bring it to a flat represenation of key, value pairs than you would need to iterate over the DOM tree (see link below). But that could cause problems in hierachical/nested XML structures, etc. Keep that in mind. Commented Aug 26, 2012 at 19:23

1 Answer 1

1

You can use DOM to parse your input XML and use something like:

import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.File;

public dumpXMLTags(...) {
  String[] keys; // you would need that with appropriate size initialized
  String[] values;  

  // Parse your XML file and construct DOM tree
  File fXmlFile = new File(PATH_TO_YOUR_XML_FILE);
  DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  Document doc = dBuilder.parse(fXmlFile);
  doc.getDocumentElement().normalize();

  // Traverse DOM tree (make sure is not empty first, etc)
  NodeIterator iterator = traversal.createNodeIterator(
      doc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);

  int i = 0;  // index to you key/value Array

  for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
     keys[i] = ((Element) n).getTagName();
     values[i] = ((Element)n).getNodeValue();
     i++;
  }
}

Alternatively you could use XPATH with the

//@* | //*[not(*)]

expression, as described here: Question 7199897

public static void main(String[] args) throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(xml)));

    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();
    NodeList nodes = (NodeList)xp.evaluate("//@* | //*[not(*)]", doc, XPathConstants.NODESET);

    System.out.println(nodes.getLength());

    for (int i=0, len=nodes.getLength(); i<len; i++) {
        Node item = nodes.item(i);
        System.out.println(item.getNodeName() + " : " + item.getTextContent());
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I think he wants to read/parse the xml and put the key-value pair in some other collection.
Ah I see, the question was edited while I was writing my answer. Than he still can use a DOM parser to retrieve the fields and feed them into a map or whatever collection is suitable.
Dear, thanks for the prompt response but I want to input these values (element node and corresponding text node) one by one to other program. I need help because in future I would not know which xml file it would be or what is the tag name. I want that any xml file be an input and output would be every element node + text node one by one.
So if I get you right, you want to dump each tag without regard of the underlying structure? Only those of a certain tag-name or simply all?
dear, yes all the tags +value one byone untill xml file ends.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.