在Android中统一读取xml响应

时间:2016-09-20 11:23:46

标签: android unity3d xmlhttprequest webrequest

我在下面得到了来自unityWebRequest的响应,用于休息api。

我想要的是每个对象的名称和价值,即表格内的名称和价值。 我想将名称用作字符串,将值用作int。

怎么做?

我看过很多教程,但是所有教程都使用了本地可用的xmldocument。

请帮助我执行此操作的代码。

提前致谢

    <HEAD>
    <TITLE>Property Listing For SimulationData</TITLE>
    <LINK rel='Stylesheet' href='/Thingworx/css/thingworxapi.css' type='text/css'></LINK>
    <META http-equiv='Content-Type' content='text/html'></META>
    <META http-equiv='cache-control' content='no-cache, no-store'></META>
    <META http-equiv='expires' content='-1'></META>
    <META http-equiv='pragma' content='no-cache, no-store'></META>
    <META http-equiv='refresh' content='30'></META>
</HEAD>
<BODY>
    <IMG SRC="/Thingworx/images/ThingworxLogo.png"/>
    <BR/>
    <H1>Property Listing For SimulationData</H1>
    <TABLE>
        <TR>
            <TH>name</TH>
            <TH>value</TH>
        </TR>
        <TR>
            <TD>ActualSpeed</TD>
            <TD>0</TD>
        </TR>
        <TR>
            <TD>AirPressure</TD>
            <TD>8.0</TD>
        </TR>
        <TR>
            <TD>Capacity</TD>
            <TD>1500.0</TD>
        </TR>
        <TR>
            <TD>Conveyor_Speed</TD>
            <TD>75.0</TD>
        </TR>
        <TR>
            <TD>CurrentTemperature</TD>
            <TD>0</TD>
        </TR>
        <TR>
            <TD>description</TD>
            <TD></TD>
        </TR>
        <TR>
            <TD>GetEquipment</TD>
            <TD>
                <TABLE>
                    <TR>
                        <TH>Machine_Name</TH>
                    </TR>
                </TABLE>
            </TD>
        </TR>
        <TR>
            <TD>IdealSpeed</TD>
            <TD>1</TD>
        </TR>
        <TR>
            <TD>isConnected</TD>
            <TD>true</TD>
        </TR>
        <TR>
            <TD>lastConnection</TD>
            <TD>2016-09-15T15&#x3a;16&#x3a;32.111&#x2b;05&#x3a;30</TD>
        </TR>
        <TR>
            <TD>lastConnectionError</TD>
            <TD></TD>
        </TR>
        <TR>
            <TD>LinearSpeed</TD>
            <TD>3.5</TD>
        </TR>
        <TR>
            <TD>Loadweight</TD>
            <TD>0.0</TD>
        </TR>
        <TR>
            <TD>MilkBikiLowerRange</TD>
            <TD>98.0</TD>
        </TR>
        <TR>
            <TD>MilkBikiUpperRange</TD>
            <TD>108.0</TD>
        </TR>
        <TR>
            <TD>name</TD>
            <TD>SimulationData</TD>
        </TR>
        <TR>
            <TD>NiceLowerRange</TD>
            <TD>91.0</TD>
        </TR>
        <TR>
            <TD>NiceUpperRange</TD>
            <TD>99.0</TD>
        </TR>
        <TR>
            <TD>NoOfRotationAgitator</TD>
            <TD>0.0</TD>
        </TR>
        <TR>
            <TD>NoofRotationConveyor</TD>
            <TD>0.0</TD>
        </TR>
        <TR>
            <TD>NoOfRotationsRotaryMould</TD>
            <TD>30.0</TD>
        </TR>
        <TR>
            <TD>NorthEast</TD>
            <TD>34.2646815,85.7826173,0.0</TD>
        </TR>
        <TR>
            <TD>NorthWest</TD>
            <TD>33.5872439,56.2797477,0.0</TD>
        </TR>
        <TR>
            <TD>OvenTemperature</TD>
            <TD>50.0</TD>
        </TR>
        <TR>
            <TD>PickPlace</TD>
            <TD>50.0</TD>
        </TR>
        <TR>
            <TD>Power</TD>
            <TD>24.0</TD>
        </TR>
        <TR>
            <TD>ScrapCount</TD>
            <TD>0</TD>
        </TR>
        <TR>
            <TD>SouthEast</TD>
            <TD>12.9036622,92.4436689,0.0</TD>
        </TR>
        <TR>
            <TD>SouthWest</TD>
            <TD>39.3923528,29.8171935,0.0</TD>
        </TR>
        <TR>
            <TD>tags</TD>
            <TD>Britania_POC&#x3a;Biscuit_POC&#x3b;FAndB_DemoKit&#x3a;F&amp;B_DemoKit</TD>
        </TR>
        <TR>
            <TD>thingTemplate</TD>
            <TD>MSSQL</TD>
        </TR>
        <TR>
            <TD>TigerLowerRange</TD>
            <TD>100.0</TD>
        </TR>
        <TR>
            <TD>TigerUpperRange</TD>
            <TD>92.0</TD>
        </TR>
        <TR>
            <TD>TotalWorkingTime</TD>
            <TD>300</TD>
        </TR>
        <TR>
            <TD>VibrationPresence</TD>
            <TD>No Vibration</TD>
        </TR>
    </TABLE>
</BODY>

1 个答案:

答案 0 :(得分:1)

您已经有回复string

你可以通过解析它来创建XMLElement

XElement response= XElement.Parse(responseString);

OR

您可以像这样创建XMLDocument

XmlDocument doc = new XmlDocument();
doc.LoadXml(responseString);

注意:您的响应字符串必须是有效的xml语法。

认为您需要使用XPath表达式来提取所需信息。

更新:

这是一个示例代码,它会给你一个想法:

void Start()
{
    string yourActualResponse = "your actual response goes here";
    StringBuilder responseString = new StringBuilder( @"<?xml version='1.0'?>");
    responseString.AppendLine(@"<reponse>");
    responseString.AppendLine(yourActualResponse);
    responseString.AppendLine(@"</reponse>");

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(responseString.ToString());

    XmlNodeList nodes = doc.DocumentElement.SelectNodes("//TR/TD");
    foreach (XmlNode node in nodes)
    {
        Debug.Log(node.InnerText);
    }
}

这里有一些入门链接:

https://msdn.microsoft.com/en-us/library/cc189056(VS.95).aspx

https://msdn.microsoft.com/en-us/library/d271ytdx(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx

希望这有帮助