我有以下XML变量&我想提取DATA中的所有值:
SELECT
x.v.value('Data[1]','VARCHAR(100)')
FROM @Xml.nodes('MetaData/Data') x(v)
- 此操作失败
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.technologybord.android.asynctasklistview.MainActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:id="@+id/progress_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:visibility="gone" />
</android.support.v7.widget.Toolbar>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
ListView mainList;
ProgressBar progressBar;
private String[] texts = {
"Apple","Mango","Peach","Banana","Orange","Grapes","Watermelon","Tomato",
"Fred", "Jane", "Richard Nixon", "Miss America",
"man", "woman", "fish", "elephant", "unicorn",
"a", "the", "every", "some",
"big", "tiny", "pretty", "bald",
"runs", "jumps", "talks", "sleeps",
"loves", "hates", "sees", "knows", "looks for", "finds",
"and", "or", "but", "because",
"Apple","Mango","Peach","Banana","Orange","Grapes","Watermelon","Tomato",
"Fred", "Jane", "Richard Nixon", "Miss America",
"man", "woman", "fish", "elephant", "unicorn",
"a", "the", "every", "some",
"big", "tiny", "pretty", "bald",
"runs", "jumps", "talks", "sleeps",
"loves", "hates", "sees", "knows", "looks for", "finds",
"and", "or", "but", "because"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//supportRequestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
mainList = (ListView)findViewById(R.id.listView1);
mainList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>()));
progressBar = (ProgressBar)findViewById(R.id.progress_spinner);
new MyTask().execute();
}
class MyTask extends AsyncTask<Void, String, Void> {
ArrayAdapter<String> adapter;
private int counter = 0;
@Override
protected void onPreExecute() {
//super.onPreExecute();
this.adapter = (ArrayAdapter<String>)mainList.getAdapter();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
progressBar.setMin(0);
}
progressBar.setMax(10000);
progressBar.setIndeterminate(false);
progressBar.setVisibility(View.VISIBLE);
//setSupportProgressBarIndeterminate(false);
//setProgressBarVisibility(true);
}
@Override
protected Void doInBackground(Void... voids) {
for(String item: texts) {
publishProgress(item);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
//super.onProgressUpdate(values);
adapter.add(values[0]);
counter++;
progressBar.setProgress((int)(((double)counter/texts.length)*10000));
}
@Override
protected void onPostExecute(Void aVoid) {
//super.onPostExecute(aVoid);
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "All items were added successfully!", Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:0)
请使用此
DECLARE @Xml XML = '<MetaData type="Contracts">
<Data column="PositionName">Administrator- Contract Compl</Data>
<Data column="PositionName">Administrator - Contract Compl</Data>
<Data column="PositionName">Dir - Contract Adm & Analytics</Data>
<Data column="PositionName">Director - Commercial Support</Data>
<Data column="PositionName">Lead Contract Admin - Compl</Data>
<Data column="PositionName">Lead Contract Administrator</Data>
<Data column="PositionName">Assoc- Contract Admin</Data>
<Data column="PositionName">Contract Administrator</Data>
<Data column="PositionName">Director- Contract Admin</Data>
<Data column="PositionName">Lead Contract Administrator</Data>
<Data column="PositionName">Manager- Contract Admin</Data>
<Data column="PositionName">Manager - Contract Admin</Data>
<Data column="PositionName">Rep - NGL Distribution</Data>
<Data column="PositionName">Supervisor- Contract Admin</Data>
<Data column="PositionName">VP- Contract Administration</Data>
<Data column="PositionName">Associate Contract Admin</Data>
<Data column="PositionName">Sr Manager - Contract Admin</Data>
<Data column="PositionName">Sr Specialist - Producer Svcs</Data>
<Data column="PositionName">Supervisor - Contract Admin</Data>
<Data column="PositionName">Supervisor - Contract Admin</Data>
<Data column="PositionName">VP - Contract Administration</Data>
</MetaData>'
DECLARE @m TABLE (Xmls XML)
INSERT INTO @m SELECT @XML
;WITH CTE AS
(
SELECT
p.value(N'.',N'nvarchar(MAX)') AS [Id]
FROM @m as x
OUTER APPLY x.Xmls.nodes(N'/MetaData/Data') AS u(p)
)
SELECT *
FROM CTE
<强>输出强>
Id
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Administrator- Contract Compl
Administrator - Contract Compl
Dir - Contract Adm & Analytics
Director - Commercial Support
Lead Contract Admin - Compl
Lead Contract Administrator
Assoc- Contract Admin
Contract Administrator
Director- Contract Admin
Lead Contract Administrator
Manager- Contract Admin
Manager - Contract Admin
Rep - NGL Distribution
Supervisor- Contract Admin
VP- Contract Administration
Associate Contract Admin
Sr Manager - Contract Admin
Sr Specialist - Producer Svcs
Supervisor - Contract Admin
Supervisor - Contract Admin
VP - Contract Administration
(21 rows affected)
我还将您的代码更改为有效。您已经提到过两次数据
DECLARE @Xml XML = '<MetaData type="Contracts">
<Data column="PositionName">Administrator- Contract Compl</Data>
<Data column="PositionName">Administrator - Contract Compl</Data>
<Data column="PositionName">Dir - Contract Adm & Analytics</Data>
<Data column="PositionName">Director - Commercial Support</Data>
<Data column="PositionName">Lead Contract Admin - Compl</Data>
<Data column="PositionName">Lead Contract Administrator</Data>
<Data column="PositionName">Assoc- Contract Admin</Data>
<Data column="PositionName">Contract Administrator</Data>
<Data column="PositionName">Director- Contract Admin</Data>
<Data column="PositionName">Lead Contract Administrator</Data>
<Data column="PositionName">Manager- Contract Admin</Data>
<Data column="PositionName">Manager - Contract Admin</Data>
<Data column="PositionName">Rep - NGL Distribution</Data>
<Data column="PositionName">Supervisor- Contract Admin</Data>
<Data column="PositionName">VP- Contract Administration</Data>
<Data column="PositionName">Associate Contract Admin</Data>
<Data column="PositionName">Sr Manager - Contract Admin</Data>
<Data column="PositionName">Sr Specialist - Producer Svcs</Data>
<Data column="PositionName">Supervisor - Contract Admin</Data>
<Data column="PositionName">Supervisor - Contract Admin</Data>
<Data column="PositionName">VP - Contract Administration</Data>
</MetaData>'
SELECT
x.v.value('.','VARCHAR(100)')
FROM @Xml.nodes('/MetaData/Data') x(v)
<强>输出强>
----------------------------------------------------------------------------------------------------
Administrator- Contract Compl
Administrator - Contract Compl
Dir - Contract Adm & Analytics
Director - Commercial Support
Lead Contract Admin - Compl
Lead Contract Administrator
Assoc- Contract Admin
Contract Administrator
Director- Contract Admin
Lead Contract Administrator
Manager- Contract Admin
Manager - Contract Admin
Rep - NGL Distribution
Supervisor- Contract Admin
VP- Contract Administration
Associate Contract Admin
Sr Manager - Contract Admin
Sr Specialist - Producer Svcs
Supervisor - Contract Admin
Supervisor - Contract Admin
VP - Contract Administration
(21 rows affected)