Tuesday, August 14, 2012

In XML, a way to extract specific tags into a string (using jdom)




Widely used recently, while the use of XML Web services are becoming frequent.

Then again, a naturally built-in XML parser in java and fine.

Javax.xml package of sub-classes for XML support this very yigetjyo the efforts of java.

However, DOM / SAX parse data processing using accomplishing without problems, but
Sub-elements, including specific tags to get the parts for strings does not support.

That is able to use it immediately yireolttae jdom.

XML test are as follows:

http://schemas.xmlsoap.org/soap/envelope/ \>




The above XML tags to fetch data from the mission will become.

Include work order
1) java parse using to create a Document. (Org.w3c.dom.Document)
2) Document object will extract only the required tags. (Doc.getElementsByTagName (tag name), use ->, the function returns a NodeList is a bundle.
3) NodeList objects in the item (index) Gets the Element you want through a function (in this Element is a org.w3c.dom.Element.)
4) org.w3c.dom.Element object (ele) of the jdom
  DOMBuilder b = new DOMBuilder ();
  org.jdom.Element element = b.build (ele);
  through the build function is to convert org.jdom.Element org.w3c.dom.Element object.
5) org.jdom.output.XMLOutputter used to prepare the output.
XMLOutputter out = new XMLOutputter ();
6) output function is output through the output stream you want.
   out.output (element, System.out);
   -> Here, so use the output stream, ByteArrayOutputStream to buffer the data and puts it on you can use a guess. However, off Disclosed On assignment to System.out outputs to standard output will be.

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
The entire source

import javax.xml.parsers. *;
import java.io. *;
import org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;
import org.w3c.dom. *;
public class XmlTest {
 public static void main (String [] args) throws Exception {
  String data = "";
  data + = "http://schemas.xmlsoap.org/soap/envelope/ \"> ";
  data + = "";
  data + = "";
  data + = "";
  data + = "";
  byte [] buffer = data.getBytes ();
  ByteArrayInputStream bai = new ByteArrayInputStream (buffer);
 
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ();
  DocumentBuilder builder = factory.newDocumentBuilder ();
 
  Document doc = builder.parse (bai);
 
  NodeList list = doc.getElementsByTagName ("data");
 
  System.out.println (list.getLength ());
  Element ele = null;
  if (list.getLength ()> 0) {
   ele = (Element) list.item (0);
   System.out.println ("list:" + ele);
  }
 
  DOMBuilder b = new DOMBuilder ();
  org.jdom.Element element = b.build (ele);
  System.out.println ("ret:" + element.getText ());
  XMLOutputter out = new XMLOutputter ();
  out.output (element, System.out);
 }
}

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
The output
list: [data: null]
ret:

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Conclusion: XML parser using the XML if you are working inside the case you need to import data into a string using the jdom source with reference to the above tasks can be accomplished.

jdom site: http://www.jdom.org/

No comments:

Post a Comment