나는 PHP의 읽기, 쓰기 및 추가를 알고 있지만… 삽입 할 수 있습니까? 또는 마지막 줄을 제거 하시겠습니까?
문제
XML 파일을 만들려고 노력하고 있지만 XML 파일은 노드로 래핑해야합니다. 따라서 사용하기 쉽지 않습니다.
이것에 대한 모든 도움이 좋을 것입니다 !!
내 XML은 두 가지 노드 유형으로 구성됩니다.
<entry id="1_0">
<title>This is the title</title>
<description>This is the description...</description>
<subName>Publishers Name</subName>
<date>Saturday, June 11th, 2007, 5:46:21 PM</date>
<BorF>bug</BorF>
</entry>
<vote id="1_0">5</vote>
그리고 jQuery를 사용하여 데이터 (현재 정적)를 PHP 파일로 전송하는 간단한 테스트 페이지를 사용합니다.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script>
$(function(){
$("#addVote").click(function() {
$.ajax({
type: "POST",
url: "saveListData.php",
data: 'url=bugs_features_listdata.xml&listData=\n<vote id="1_2">3</vote>',
async: false,
cache: false,
success: function(data, textStatus) {
if (window.console) console.log(textStatus);
},
complete: function(XMLHttpRequest, textStatus){
if (window.console) console.log(textStatus);
}
});
});
$("#addEntry").click(function() {
$.ajax({
type: "POST",
url: "saveListData.php",
data: 'url=bugs_features_listdata.xml&listData=\n<entry id="1_1">\n\
\t<title>This is the title 1</title>\n\
\t<description>This is the description...</description>\n\
\t<subName>Publishers Name</subName>\n\
\t<date>Saturday, June 11th, 2007, 5:46:21 PM</date>\n\
\t<BorF>bug</BorF>\n\
</entry>',
async: false,
cache: false,
success: function(data, textStatus) {
if (window.console) console.log(textStatus);
},
complete: function(XMLHttpRequest, textStatus){
if (window.console) console.log(textStatus);
}
});
});
});
</script>
</head>
<body>
<a id="addVote" href="#">ADD VOTE</a><br /><br /><a id="addEntry" href="#">ADD ENTRY</a>
</body>
</html>
... 현재 XML 파일에 추가되지만 시작/끝 노드가 필요합니다.
해결책
TXWINGE는 귀하의 질문에 대한 그의 의견에서 올바른 아이디어를 가지고있었습니다. 많은 내장 XML 조작 라이브러리 중 하나를 사용하여 노드를 추가 한 다음 직렬화하고 텍스트 파일로 저장해야합니다. ~ 안에 단순 렉스, 예를 들어:
$xml = simplexml_load_file('test.xml');
$vote = $xml->addChild('vote', '5');
$vote->addAttribute('id','1_0');
$fp = fopen('test.xml', 'w');
fwrite($fp, $xml->asXML());
fclose($fp);
거기 있습니다 다른 XML 조작 라이브러리 그것은 당신의 과제에 더 잘 맞을 수 있습니다
다른 팁
XML 문서의 구조에 달려 있다고 말합니다. "REST"가 예측 가능하면 파일에서 해당 부분을 자르고 새 데이터를 추가하고 "REST"를 다시 붙여 넣을 수 있습니다.
⋮
n-1: <node>last node</node>
n : </root>
⋮
n-1: <node>last node</node>
n : <node>new node</node>
n+1: </root>
이 작업을 수행하려고하는 경우 빠르고 더러운 방법 : "r+"에서 파일을 열고 파일의 마지막 요소와 파일의 끝 사이의 빈 줄로 포인터를 설정하면 루트 레벨 요소가 닫히는 동안 쓰기됩니다. 태그, 새 자식 요소를 작성한 후 다시 추가하십시오. 4 시간 동안 PHP 스크립트를 작성해 왔기 때문에 더 나은 방법이있을 것입니다. 그러나 이것은 4 시간 경력에서 가장 간단한 것입니다.
$name = $_POST["name"];
$phone = $_POST["phone"];
$fh = fopen("xml-test-1.xml", "r+");
if (!$fh)
{
die("Failed to open");
}
fseek($fh, -11, SEEK_END);
fwrite($fh, "<person> \n");
fwrite($fh, "<name>$name</name> \n");
fwrite($fh, "<phone>$phone</phone> \n");
fwrite($fh, "</person> \n");
fwrite($fh, "</people> \n");
fclose($fh);
echo "now look and see if anything was added";
제휴하지 않습니다 StackOverflow