PhpÀûÓÃjava½âÎöxml
Ê×ÏÈÈç¹ûÄã¶ÔJAVA²»¸ÐÐËȤ»òÕß˵ÄãµÄÖ÷»ú²»Ö§³ÖJAVA»òÕßÄãÒ²²»´òËãѧϰµÄ»°Çë²»ÒªÀË·Ñʱ¼äÔÚÕâÀï¡£ÇëÏȰ²×°JAVAµÄÖ´Ðл·¾³ÓëPHP½áºÏ£¬¾ßÌå²Î¿¼[url]http://www.phpx.com/happy/thr78795.html[/url]
»òÕßÏÂÔØ[url]http://www.javax.org/download/php_java.rar[/url]
ÀïÃæÎÒдÓиöreadme.txt˵Ã÷Îĵµ£¬Õâ¸öѹËõ°üÊǸö¼òµ¥µÄÀý×Ó¡£
ÒªÏÂÔØÎÒµÄJAVAÔ´ÂëÇëµ½[url]http://www.javax.org/download/JavaXml.rar[/url]
ÓÐÆäËûÎÊÌâÇëµ½[url]http://www.javax.org/ÌáÎÊ¡£[/url]
Ò»¿ªÊ¼ÎÒÏëÓÃPHP½âÎöXML£¬µ«·¢ÏÖPHPÒª½âÎöXMLÓеãÂé·³£¬ºÃÏóPHP5±È½Ï·½±ãÁË£¬µ«ÎÒÕâ±ßûÓÐ×°5£¬»¹ÊÇ4.3¡£ºóÀ´ÎÒ¿´µ½IBMһƪÎÄÕÂ([url]http://www-900.cn.ibm.com/developer...kit/index.shtml[/url])½²µ½¿ÉÒÔÀûÓÃJAVAÀ´×ö£¬ËùÒÔÎÒÏÖÔÚ¾ÍÊÔÑéÁËһϡ£
Òª½âÎöµÄXMLÎļþ£ºfirst.xml£¬ÄÚÈÝÈçÏ£º
<?xml version="1.0" encoding="UTF-8"?>
<product>
<hello>
<name>Ð¡Ôø</name>
<age>20</age>
</hello>
<hello>
<name>ÓêÉ¡</name>
<age>20</age>
</hello>
</product>
ÆäÖÐ<product>ÊǸù£¬<hello>Êǽڵ㣬һ¹²ÓÐÁ½¸ö£¬nameÖµÃû×Ö²»Ò»Ñù£¬Ò»¸öÊÇÐ¡ÔøÒ»¸öÊÇÓêÉ¡£¬ÄêÁäÒ»Ñù¡£
ÏÂÃæ¿´PHP½âÎöʱºòµÄÎļþ£º
<?php
/*
×÷ÕߣºÓêÉ¡
ʱ¼ä£º2004/12/28
*/
$JavaXml = new Java("JavaXml"); //ÕâÀïÊÇÉú³ÉÒ»¸öÎÒдµÄJAVA½âÎöXMLÊý¾ÝµÄÀà
$JavaXml->init(); //ÕâÀïΪ³õʼ»¯£¬±ÈÈçÈ¡global.propertiesÎļþÀïµÄXMLÎļþĿ¼£¨µ±È»ÄãÏÂÔØÁËÀý×ÓÒÔºóÒª¸Ä³ÉÄãµÄXMLÎļþĿ¼£©
$JavaXml->Parse("first.xml"); //Ö¸¶¨Òª½âÎöµÄÎļþ£¬Ïà¶ÔÓÚglobal.propertiesÎļþÀïÖ¸¶¨µÄĿ¼ÏÂ
$JavaXml->get(0); //ÕâÀïΪȡµÃµÚÒ»¸ö½Úµã
echo $JavaXml->getValue("name")."<br>"; //È¡µÃµÚÒ»¸ö½Úµãname±êǩֵ
echo $JavaXml->getValue("age")."<br>"; //È¡µÃµÚÒ»¸ö½Úµãage±êǩֵ
$JavaXml->setValue("name","´óÍ·°Ö°Ö"); //ÉèÖõÚÒ»¸ö½Úµãname±êǩֵΪ´óÍ·°Ö°Ö
$JavaXml->get(1); //ÕâÀïΪȡµÃµÚ¶þ¸ö½Úµã
echo $JavaXml->getValue("name")."<br>"; //È¡µÃµÚ¶þ¸ö½Úµãname±êǩֵ
echo $JavaXml->getValue("age")."<br>"; //È¡µÃµÚ¶þ¸ö½Úµãage±êǩֵ
?>
$JavaXml->get(0);È¡µÃ½ÚµãλÖ㬱ÈÈçÎÒµÄXMLÎļþÓÐÁ½×é<hello>£¬ÕâÀïget(0)µÄ»°¾ÍÊÇÈ¡µÚÒ»×éµÄ<hello> £¬get(1)¾ÍÊÇÈ¡µÚ¶þ×éµÄ<hello>.
×îºóÊä³öÊÇ
Ð¡Ôø
20
ÓêÉ¡
20
ÒòΪ$JavaXml->setValue("name","´óÍ·°Ö°Ö"); Õâ¾äÐÞ¸ÄÁ˵ÚÒ»¸ö½ÚµãµÄname±êÇ©µÄÖµ£¬XMLÎļþÒѾ±»¸üйýÁË£¬ËùÒÔµ±ÔÙÖ´ÐÐÒ»´ÎÕâ¸öPHPÎļþµÄʱºò½á¹û»á³ÉΪ
´óÍ·°Ö°Ö
20
ÓêÉ¡
20
ÒÔÉϼòµ¥¼¸¾ä¾Í½âÎöÍêÁË£¬ÏÂÃæÊÇÎÒµÄJAVAÀ࣬ÀïÃæÓõ½ÁËJDOMÀ´½âÎöXML¡£
import org.jdom.* ;
import org.jdom.output.* ;
import org.jdom.input.* ;
import java.io.* ;
import java.util.*;
/*
×÷ÕߣºÓêÉ¡
ʱ¼ä£º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();
}
}
Èç¹û»¹ÓÐʲô²»¶®µÄÇë¸úÌù,ÕâÑù×Ó¶ÔÐÂÊÖ²»ÖªµÀÊǸüÂé·³»¹ÊǸü·½±ãÁË£¬Çë´ó¼Ò·¢±í×Ô¼ºµÄÒâ¼û£¬Èç¹û¸÷λ¾õµÃ¿ÉÒԵϰÎÒ½«¼ÌÐøÍêÉÆÄǸöJAVAÀ࣬Ìṩ¸ü¶àµÄ½âÎöXMLµÄ¹¦ÄÜ¡£ ²»ÊÇ¿ÉÒÔÓÐ×Ô¼ºµÄclassô£¬²»ÓÃjavaµÄÒ²ÊÇ¿ÉÒԵİɣ¬¼ÇµÃºÃ¶àÈ˶¼Ð´ÁËÀàËÆµÄ¶«Î÷£ºP
Ò³:
[1]