PHP using Java parsing xml_php tutorial

Source: Internet
Author: User
Please first install the Java execution Environment and PHP combination, the specific reference http://www.phpx.com/happy/thr78795.html
or download Http://www.javax.org/download/php_java.rar
Inside I wrote a readme.txt document, this compression package is a simple example.
To download my Java source, please go to Http://www.javax.org/download/JavaXml.rar.
If you have any other questions, please ask http://www.javax.org/.

At first I want to parse XML with PHP, but found that PHP to parse XML is a bit cumbersome, as if PHP5 more convenient, but I do not install 5, or 4.3. Later I saw an IBM article (http://www-900.cn.ibm.com/developer...kit/index.shtml) that I could do with Java, so I tried it now.
XML file to parse: First.xml, which reads as follows:



Xiao Zeng
20


Umbrella
20


which is the root, is a node, a total of two, name value is not the same, one is small once an umbrella, the same age.
Here's a look at the PHP parsing file:
$JAVAXML = new Java ("Javaxml"); Here is a class that generates a Java parsing XML data that I wrote
$JavaXml->init (); Here is the initialization, such as fetching the XML file directory in the Global.properties file (of course, you download the example later to change to your XML file directory)
$JavaXml->parse ("First.xml"); Specifies the file to parse, relative to the directory specified in the Global.properties file.
$JavaXml->get (0); Here for the first node to get
echo $JavaXml->getvalue ("name"). "
"; Gets the first node name tag value
echo $JavaXml->getvalue ("Age"). "
"; Gets the first node of the age Tag value
$JavaXml->setvalue ("name", "Big Head Daddy"); Set the first node name tag value for Big head dad
$JavaXml->get (1); To get the second node here
echo $JavaXml->getvalue ("name"). "
"; Get second node name tag value
echo $JavaXml->getvalue ("Age"). "
"; Get second node age tag value
?>

$JavaXml->get (0); Get node position, for example, my XML file has two groups , here get (0) is to take the first set of , get (1) takes the second set of .
The final output is
Xiao Zeng
20
Umbrella
20
Because $javaxml->setvalue ("name", "Big Head Daddy"); This modifies the value of the first node's name tag, and the XML file has been updated so that when the PHP file is executed again, the result becomes
Big Head Daddy
20
Umbrella
20

The above simple sentence is resolved, the following is my Java class, which used the 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 ();
}
}

http://www.bkjia.com/PHPjc/532644.html www.bkjia.com true http://www.bkjia.com/PHPjc/532644.html techarticle Please first install the Java execution Environment and PHP combination, specific reference http://www.phpx.com/happy/thr78795.html or download http://www.javax.org/download/php_ Java.rar inside I wrote a readme.txt said ...

  • 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: info-contact@alibabacloud.com 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.