Php parses xml using java. Pipeline said please first install JAVA execution environment and PHP combined, specific reference http://www.phpx.com/happy/thr78795.html
Or download http://www.javax.org/download/php_java.rar
I have written a readme.txt instruction document. this compressed package is a simple example.
To download my JAVA source code please go to http://www.javax.org/download/JavaXml.rar
If you have other questions, go to http://www.javax.org/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 (http://www-900.cn.ibm.com/developer...kit/index.shtml) about using JAVA, so now I 've tried it.
The XML file to be parsed: first. xml. the content is as follows:
Mr. Zeng
20
Umbrella
20
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
20
Umbrella
20
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
20
Umbrella
20
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 http://www.javax.org/download/php_java.rar. I wrote a readme.txt saying...