我需要为产品生成整数 ID,然后通过输出中的整数 ID 引用相关产品。在输入中,我有代表这种关系的字符串键。谢谢您的帮助。
输入:
<root>
<products>
<product>
<!-- a unique string key of this node between the other product nodes -->
<stringKey>AppleRef</stringKey>
<Name>Apple</Name>
<relatedProducts>
<!-- a reference to product/StringKey of Orange -->
<relatedProductStringKey>OrangeRef</relatedProductStringKey>
<!-- other related products may follow -->
</relatedProducts>
</product>
<product>
<stringKey>OrangeRef</stringKey>
<Name>Orange</Name>
<relatedProducts>
<relatedProductStringKey>AppleRef</relatedProductStringKey>
</relatedProducts>
</product>
</products>
</root>
预期输出:
<root>
<products>
<P>
<ProductInfo>
<!-- a unique integer ID of this node between the other ProductsInfo nodes -->
<ProductID>0</ProductID>
<ProductRef>AppleRef</ProductRef>
<ProductName>Apple</ProductName>
</ProductInfo>
<R>
<ProductRelatedInfo>
<!-- a unique integer ID of this node between the other ProductRelatedInfo nodes -->
<RelatedID>0</RelatedID>
<!-- a reference to ProductInfo/ProductID of Orange -->
<RelatedProductID>1</RelatedProductID>
</ProductRelatedInfo>
<!-- other related products may follow -->
</R>
</P>
<P>
<ProductInfo>
<ProductID>1</ProductID>
<ProductRef>OrangeRef</ProductRef>
<ProductName>Orange</ProductName>
</ProductInfo>
<R>
<ProductRelatedInfo>
<RelatedID>1</RelatedID>
<RelatedProductID>0</RelatedProductID>
</ProductRelatedInfo>
</R>
</P>
</products>
</root>