Php uses java to parse xml_PHP tutorial

WBOY
Release: 2016-07-13 17:19:31
Original
885 people have browsed it

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>
Xiao Zeng
20

Umbrella
20


Where is the root, is There are two nodes in total. The name values ​​are different. One is Xiao Zeng and the other is Umbrella. They have the same age.
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 . Here, get(0) will get the of the first group, and get(1) will get the of the first group. Group 2.
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(); 


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/532644.htmlTechArticle请先安装JAVA的执行环境与PHP结合,具体参考http://www.phpx.com/happy/thr78795.html 或者下载http://www.javax.org/download/php_java.rar 里面我写有个readme.txt说...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!