I want to parse an XML having same parent-child tag and then link the value of parent tag to child tag using preferably SAX parser.
我想解析具有相同父子标记的XML,然后使用SAX解析器将父标记的值链接到子标记。
This is the XML file
这是XML文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- Protocol header -->
<Protocol id="Diameter customer, country" spec="RFC3588"
name="Diameter" version="1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:/$CLASSES/cmg/stdapp/diameter/validation/Diameter_addon_schema.xsd">
<!-- ACR declaration: Start -->
<Request name="Start">
<Condition key="Accounting-Record-Type" value="2"/>
<AVP name="Node-Id" defaultValue="MTAS"/>
<AVP name="Session-Id"/>
<AVP name="Origin-Host"/>
<AVP name="Subscription-Id">
<AVP name="Subscription-Id-Type"/>
<AVP name="Subscription-Id-Data"/>
</AVP>
<AVP name="IMS-Information">
<AVP name="Event-Type">
<AVP name="SIP-Method"/>
</AVP>
<AVP name="Role-of-Node"/>
</AVP>
</Request>
<!---->
</Protocol>
In this example a tag with name AVP
has child tag with same name AVP
. I want to get the value of attribute name and then associate the value of parent to that of child. I use SAX parser for this but I'm unable to distinguish between parent and child but there is no distinction parent and child tags.
在此示例中,名称为AVP的标记具有同名AVP的子标记。我想获取属性名称的值,然后将parent的值与child的值相关联。我使用SAX解析器,但我无法区分父和子,但父和子标签没有区别。
Java Code is
Java代码是
public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException
{
if (elementName.equalsIgnoreCase("AVP"))
{
AVP_Tmp = new AVP();
String nameValue = attributes.getValue("name");
if (nameValue == null)
{
nameValue =attributes.getValue("value");
}
if (nameValue != null)
{
AVP_Tmp.set(nameValue,elementName,attributes);
}
}
}
@Override
public void endElement(String s, String s1, String element) throws SAXException
{
if (element.equals("AVP"))
{
Object key = AVP_Tmp.tmpValue;
Object value = AVP_Tmp.tmpValue;
AVPL.put(key, value);
}
}
The AVP_Tmp
is a class whose set method is as follows:
AVP_Tmp是一个类,其set方法如下:
public void set(String nameValue, String qName, Attributes attrs)//, int k)
{
int len = attrs.getLength();
tmpValue=qName + "-->" + nameValue;
List list = new ArrayList();
for (int i = 0; i < len; i++)
{
list.add(attrs.getQName(i));
}
Collections.sort(list);
for (int i = 0; i < len; i++)
{
name1[i]= (Object)list.get(i);
value1[i]=(attrs.getValue((String) list.get(i)));
tmpValue=tmpValue+ "\n" +name1[i]+"="+value1[i];
}
}
I currently have output as:
我目前的输出为:
Node-Id
..
..
Subscription-Id
Subscription-Id-Type
IMS-Information
Event-Type
SIP-Method
..
I want the Output in format like:
我希望输出格式如下:
Node-Id
..
..
..
Subscription-Id#Subscription-Id-Type
IMS-Information#Event-Type#SIP-Method
..
1 个解决方案
#1
0
When I got your goals right, I would do it the way, that you build the "AVP-Structure" and afterwards extract the needed output.
当我实现你的目标时,我会这样做,你建立“AVP结构”,然后提取所需的输出。
So when a new AVP starts it will look something like that (just in pseudo-code):
因此,当一个新的AVP启动时,它将看起来像那样(只是在伪代码中):
if (parent == null){
avpTemp = new AVP();
parent = avpTemp;
} else {avpTemp = new AVP(parent); }
After the parsing, you have got the hierarchy and build the structe/result as you want it.
解析后,您已获得层次结构并根据需要构建结构/结果。
#1
0
When I got your goals right, I would do it the way, that you build the "AVP-Structure" and afterwards extract the needed output.
当我实现你的目标时,我会这样做,你建立“AVP结构”,然后提取所需的输出。
So when a new AVP starts it will look something like that (just in pseudo-code):
因此,当一个新的AVP启动时,它将看起来像那样(只是在伪代码中):
if (parent == null){
avpTemp = new AVP();
parent = avpTemp;
} else {avpTemp = new AVP(parent); }
After the parsing, you have got the hierarchy and build the structe/result as you want it.
解析后,您已获得层次结构并根据需要构建结构/结果。