You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何处理包含两个<?xml version='1.0'?>声明的XML文件?

处理含多个XML声明的文件:解析与拆分方案

这个问题很常见——你拿到的内容是格式不合法的XML,因为XML规范明确要求一个文档只能有一个<?xml version='1.0'?>声明,多个声明会直接导致标准XML解析器报错,无法正常读取。下面给你两种可行的解决思路:

一、按XML声明拆分文件

这是最直接的方式,把内容拆成两个独立的合法XML文件:

1. 手动拆分

找到第二个<?xml version='1.0'?>的位置,将内容分割为两部分:

  • 第一部分:保留第一个XML声明 + <Properties>及其内部所有内容,保存为properties.xml
  • 第二部分:保留第二个XML声明 + <Images>及其内部所有内容,保存为images.xml

2. 脚本自动拆分(以Python为例)

如果需要批量处理这类文件,可以写简单脚本自动分割:

with open("invalid_combined.xml", "r") as input_file:
    full_content = input_file.read()

# 按XML声明分割,过滤掉空的片段
xml_segments = [seg.strip() for seg in full_content.split("<?xml version='1.0'?>") if seg.strip()]

# 为每个片段添加XML声明,保存为独立文件
for index, segment in enumerate(xml_segments, 1):
    with open(f"valid_xml_part_{index}.xml", "w") as output_file:
        output_file.write("<?xml version='1.0'?>\n" + segment)

二、预处理后直接解析

如果不想拆分文件,也可以先修复格式,让标准解析器能读取:

方法:添加统一根节点

移除多余的XML声明,然后用一个自定义根节点(比如<Root>)包裹原有的<Properties>和<Images>节点,处理后的合法XML如下:

<?xml version='1.0'?>
<Root>
<Properties>
<Property>
<Prop_Class>Residential</Prop_Class>
<Prop_RefId>Resads - FHGS - 2034 - 160 - 067546</Prop_RefId>
<Prop_CompanyGroup>ma</Prop_CompanyGroup>
<Prop_CompanyName>Propertysvhavs</Prop_CompanyName>
<Prop_Locality>30</Prop_Locality>
<Prop_Address1>3 Bedroom houses</Prop_Address1>
<Prop_Address2></Prop_Address2>
<Prop_Address3>Clare Road</Prop_Address3>
<Prop_Address4></Prop_Address4>
<Prop_Eircode></Prop_Eircode>
<Prop_Latitude>533.3498</Prop_Latitude >
<Prop_Longitude>623.260300000000029</Prop_Longitude >
<Prop_Status>A</Prop_Status>
<Prop_SaleOrRent>Sale</Prop_SaleOrRent>
<Prop_SaleType>For Sale</Prop_SaleType>
<Prop_Type>Residential Apartment</Prop_Type>
<Prop_Bedrooms>1</Prop_Bedrooms>
<Prop_Bathrooms>3</Prop_Bathrooms>
<Prop_FullDescription></Prop_FullDescription>
<Prop_Price></Prop_Price>
<Prop_PriceOption>m</Prop_PriceOption>
<Prop_ShowPrice>Y</Prop_ShowPrice>
<Prop_Negotiator>Philip O'Reilly - Test </Prop_Negotiator>
<Prop_EnergyRating>A2</Prop_EnergyRating>
<Prop_EnergyRatingDetails>A2</Prop_EnergyRatingDetails>
</Property>
</Properties>
<Images>
<Image>
<Prim_RefId>Resads - FHGS - 2034 - 160 - 067546</Prim_RefId>
<Prim_CompanyGroup>ma</Prim_CompanyGroup>
<Prim_Type>PA</Prim_Type>
<Prim_Filename>http://www.prhjsgdh.ie/uploads/web/286_3 bed dev.jpg</Prim_Filename>
<Prim_Status>A</Prim_Status>
<Prim_Class>Residential</Prim_Class>
</Image>
<Image>
<Prim_RefId>Resads - FHGS - 2034 - 160 - 067546</Prim_RefId>
<Prim_CompanyGroup>ma</Prim_CompanyGroup>
<Prim_Type>PA</Prim_Type>
<Prim_Filename>http://www.hashjshd.ie/uploads/web/286_3bedsemi-2014.jpg</Prim_Filename>
<Prim_Status>A</Prim_Status>
<Prim_Class>Residential</Prim_Class>
</Image>
<Image>
<Prim_RefId>Resads - FHGS - 2034 - 160 - 067546</Prim_RefId>
<Prim_CompanyGroup>ma</Prim_CompanyGroup>
<Prim_Type>PA</Prim_Type>
<Prim_Filename>http://www.asdbjhsdh.ie/uploads/web/286_3 bed dev.jpg</Prim_Filename>
<Prim_Status>A</Prim_Status>
<Prim_Class>Residential</Prim_Class>
</Image>
</Images>
</Root>

处理完成后,任何标准XML解析器都能正常读取并解析这个文件。

额外建议

如果这个内容是从第三方服务获取的,最好和服务提供者沟通,让他们修复输出格式——毕竟生成包含多个XML声明的内容本身是不符合规范的,从源头解决能避免后续重复处理这类问题。

内容的提问来源于stack exchange,提问作者Syed mohamed aladeen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:43:49