PHP parses XML with Java

Source: Internet
Author: User
Tags php file rar
XML Author: umbrella
First of all, if you are not interested in Java or if your host does not support Java or you are not planning to learn, please do not waste time here.

Please install the Java Execution Environment and PHP in conjunction with the specific reference http://www.phpx.com/happy/thr78795.html

or download Http://www.javax.org/download/php_java.rar
I have a Readme.txt document in it, and this package is a simple example.

To download my Java source, go to Http://www.javax.org/download/JavaXml.rar.

If you have any other questions, please ask http://www.javax.org/.


At first I wanted to parse XML with PHP, but found PHP to parse XML a little bit of trouble, as if PHP5 more convenient, but I did not install 5, or 4.3. Then 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.

The XML file to parse: First.xml, which reads as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<product>
<name> Little Ever </name>
<age>20</age>
<name> Umbrellas </name>
<age>20</age>
</product>
Where <product> is the root,
Here's a look at the PHP parsing file:

<?php
/*
Author: umbrella
Time: 2004/12/28
*/

$JAVAXML = new Java ("Javaxml"); Here is a class that generates a Java parsing XML data that I write
$JavaXml->init (); Here for initialization, such as the XML file directory in the Global.properties file (of course, you download the example 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 getting the first node
echo $JavaXml->getvalue ("name"). " <br> "; Gets the first node name label value
echo $JavaXml->getvalue ("Age"). " <br> "; Get the first node age label value
$JavaXml->setvalue ("name", "Big Head Dad"); Set the first node name tag value for the Big head dad
$JavaXml->get (1); Here for getting a second node
echo $JavaXml->getvalue ("name"). " <br> "; Gets the second node name label value
echo $JavaXml->getvalue ("Age"). " <br> "; Get the second Node age label value
?>


$JavaXml->get (0), to get the node location, such as my XML file has two sets of
The final output is
Xiao Zeng
20
Umbrella
20

Because $javaxml->setvalue ("name", "Big Head Dad"); This modifies the value of the first node's name tag, and the XML file has been updated, so when you execute the PHP file again, the result becomes
Big Head, Dad.
20
Umbrella
20


The above simple sentence is resolved, the following is my Java class, which uses the jdom to parse XML.

Import org.jdom.*;
Import org.jdom.output.*;
Import org.jdom.input.*;
Import java.io.*;
Import java.util.*;
/*
Author: umbrella
Time: 2004/12/28
*/
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 ();
}

}


If there is any do not understand please keep abreast, this kind of novice do not know is more trouble or more convenient, please express their views, if you feel that I will continue to refine that Java class, to provide more analytic XML functionality.

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.