Home > Article > Backend Development > Php uses java to parse xml_PHP tutorial
Please first install the JAVA execution environment and combine it with PHP. For details, please refer to http://www.phpx.com/happy/thr78795.html
or download http://www.javax.org/download/php_java.rar
I wrote a readme.txt description document inside, and 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, please go to http://www.javax.org/ to ask.
At first I wanted to use PHP to parse XML, but found that it was a bit troublesome for PHP to parse XML. It seemed that PHP5 was more convenient, but I did not install 5 here, so it was still 4.3. Later, I saw an IBM article (http://www-900.cn.ibm.com/developer...kit/index.shtml) that mentioned that JAVA can be used, so I will try it now.
The XML file to be parsed: first.xml, the content is as follows:
< hello>
Where
Look at the file when parsed by PHP:
$JavaXml = new Java("JavaXml"); //Here is a class that generates a JAVA class I wrote to parse XML data
$JavaXml->init(); //Here is initialization, such as taking 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); //Here is to get the first node
echo $JavaXml->getValue("name")."
"; //Get the name tag value of the first node
echo $JavaXml->getValue("age")."
"; //Get the age tag value of the first node
$JavaXml->setValue("name","Big Head Dad"); //Set the name tag value of the first node to Big Head Dad
$JavaXml->get(1); //Get the second node here
echo $JavaXml->getValue("name")."
"; //Get the second node Node name tag value
echo $JavaXml->getValue("age")."
"; //Get the second node age tag value
?>
$ JavaXml->get(0); gets the node position. For example, my XML file has two groups of
The final output is
Xiao Zeng
20
Umbrella
20
Because $JavaXml->setValue("name","Big Head Dad" ); This sentence modifies the value of the name tag of the first node. The XML file has been updated, so when the PHP file is executed again, the result will become
Big Head Daddy
20
Umbrella
20
The above simple sentences have completed the analysis. 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();
}
}