PHP解析bpmn生成的XML文件会有数据丢失
这是用xml_parser解析
$xmlfile = 'test.xml';
$xmlparser = xml_parser_create();
// 打开文件并读取数据
$fp = fopen($xmlfile, 'r');
$xmldata = fread($fp, 4096);
xml_parse_into_struct($xmlparser,$xmldata,$values);
xml_parser_free($xmlparser);
echo json_encode($values);
这是用别人递归方式解析
//对xml文件进行解析
function get_xml_file($filename = 'test.xml'){
//首先要建一个DOMDocument对象
$domObject = new DOMDocument();
//加载test.xml文件
$domObject->load($filename);
//返回xml文件的根节点
$rootObject = $domObject->documentElement;
//根据跟节点取到xml的结构返回一个数组;
$array = get_node_array($rootObject);
//返回xml结构,类型为数组
return $array;
}
//返回一个xml节点的所以子元素
function get_node_array($node)
{
//定义数组返回这个xml的结构
$array = array();
//判断根节点是否还有属性
if($node->hasAttributes())
{
foreach($node->attributes as $attrabute)
{
$array[$attrabute->nodeName]=$attrabute->nodeValue;
}
}
//如果根节点还有子节点
if($node->hasChildNodes())
{
//取到根节点的所有子节点,返回一数组
$child_array = $node->childNodes;
//如果只用一个子节点
if($child_array->length == 1)
{
$array[$node->firstChild->nodeName]=$node->firstChild->nodeValue;
}else
{
//开始遍历子节点
foreach($child_array as $child_item)
{
//如果这个节点不是文本节点的话,递归进行遍历
if($child_item->nodeType != XML_TEXT_NODE)
{
$array[$child_item->nodeName][] = get_node_array($child_item);
}
}
}
}else
{
return $node->nodeValue;
}
return $array;
}
print_r(json_encode(get_xml_file('test.xml')));
具体的xml文件,test.xml
_6-450
_6-652
_6-674
_6-695
_6-463
_6-514
_6-565
_6-616
_6-630
_6-630
_6-691
_6-693
_6-691
_6-746
_6-748
_6-748
_6-746
_6-693
_6-632
_6-632
_6-634
_6-634
_6-636
_6-636
_6-125
_6-125
_6-178
_6-178
_6-420
_6-420
_6-430
_6-422
_6-424
_6-422
_6-428
_6-424
_6-426
_6-426
_6-430
_6-428
_6-434
_6-434
_6-436
_6-436
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
try
$data = json_decode(json_encode(simplexml_load_file('test.xml', 'SimpleXMLElement', LIBXML_NOCDATA)), true);