How to parse xml_PHP using java

Source: Internet
Author: User
Php parses xml using java. Pipeline said please first install JAVA execution environment and PHP combined, specific reference
Or download
I have written a readme.txt instruction document. this compressed package is a simple example.
To download my JAVA source code please go to
If you have other questions, go to ask.

At first, I wanted to use PHP to parse XML, but I found it a little troublesome to parse XML. it seems that PHP5 is more convenient, but I didn't install 5 or 4.3 here. Later I saw an IBM article ( about using JAVA, so now I 've tried it.
The XML file to be parsed: first. xml. the content is as follows:

Mr. Zeng


Where Is root, It is a node. There are two in total. the name values are different. one is Xiaozeng, the other is an umbrella, and the old is the same.
The file for parsing in PHP is as follows:
$ JavaXml = new Java ("JavaXml"); // A class for parsing XML data written in JAVA is generated here.
$ JavaXml-> init (); // This is initialization, for example, to retrieve the XML file directory in the global. properties File (of course, you need to change it to your XML file directory after downloading the example)
$ JavaXml-> Parse ("first. xml"); // specify the file to be parsed, relative to the directory specified in the global. properties file
$ JavaXml-> get (0); // obtain the first node.
Echo $ JavaXml-> getValue ("name ")."
"; // Obtain the first node name tag value
Echo $ JavaXml-> getValue ("age ")."
"; // Obtain The age label value of the first node
$ JavaXml-> setValue ("name", ""); // you can specify the name label of the first node as "".
$ JavaXml-> get (1); // Obtain the second node.
Echo $ JavaXml-> getValue ("name ")."
"; // Obtain the second node name tag value
Echo $ JavaXml-> getValue ("age ")."
"; // Get the second node age tag value

$ JavaXml-> get (0); get the node location. for example, my XML file has two groups. Here, get (0) is the first group , Get (1) is to take the second group .
The final output is
Mr. Zeng
Because $ JavaXml-> setValue ("name", ""); this statement modifies the name tag value of the first node. the XML file has been updated, so when you execute this php file again, the result will be
Big data dad

The above simple sentence is parsed. below is my JAVA class, which uses JDOM to parse XML.
Import org. jdom .*;
Import org. jdom. output .*;
Import org. jdom. input .*;
Import java. io .*;
Import java. util .*;
Public class JavaXml {
Public String path = null;
Public String XmlFileName = null;
Public SAXBuilder sax = null;
Public Document doc = null;
Public Element root = null;
Public List xlist = null;
Public Element e = null;
Public Element value = null;
Public String getTest (){
Return new String ("haha ");
Public JavaXml (){
Public String init (){
InputStream is = getClass (). getResourceAsStream ("global. properties ");
Properties dbProps = new Properties ();
Try {
DbProps. load (is );
Catch (Exception e ){
Return ("error file ");
This. path = dbProps. getProperty ("XmlPath ");
Return ("OK ");
Public void get (int child ){
This. e = (Element) xlist. get (child );
Public String getValue (String name ){
This. value = e. getChild (name );
Return this. value. getText ();
Public void setValue (String name, String value) throws Exception {
This. value = e. getChild (name );
This. value. setText (value );
XMLOutputter xmlout = new XMLOutputter ();
Xmlout. output (doc, new FileOutputStream (path + XmlFileName ));
Public void Parse (String XmlFileName)
Throws Exception
This. XmlFileName = XmlFileName;
This. sax = new SAXBuilder ();
This.doc = sax. build (new FileInputStream (path + XmlFileName ));
This. root = doc. getRootElement ();
This. xlist = root. getChildren ();

Or download the I wrote a readme.txt saying...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.